Advertisement
yo2man

Introduction to RxJava Notes

Jul 4th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. //Introduction to RxJava Notes
  2.  
  3. RxJava lets is powerful and elegant. It lets you do what would take many lines of code into like three lines.
  4.  
  5. Here is a teaser:
  6.  
  7. // Whenever users type in text into the the SearchWidget in the Actionbar, the database is re-queried using the entered text and the results are displayed in a list below the Actionbar.
  8. Now, suppose that you’re supposed to implement something like this for an app you’re building, but suppose that there are some further requirements, namely, that the query should only execute if:
  9.  
  10. there are at least three characters entered into the SearchWidget
  11. there has been at least a 100 millisecond delay before any other characters have been entered into the SearchWidget
  12.  
  13. // How many lines of code do you think it would take to implement something like this? Around 50 for regular.
  14.  
  15. // Here’s what’s awesome about RxJava: Once you have an RxJava Observable that’s set up to report events about text changes in the SearchWidget, you can do the equivalent of all of this in three lines of code. The RxJava way of doing this, moreover, will provide you with more flexibility that will help you cope with any subsequent changes you’ll need to make to this portion of the code.
  16.  
  17.  
  18.  
  19. Synchronous data = wait for it to run = Phone Call conversation. You say "hi, how was your day girl?" and do nothing else as you wait for the girl to reply.
  20. Asynchronous data = runs in background while waiting for reply = like Text Message conversation
  21.  
  22. // RxJava let’s you deliver asynchronous data to anyone who’s interested in receiving it.
  23. // Of course, RxJava does more than that, but, this is definitely something that you can do with RxJava.
  24.  
  25.  
  26. Notice that an 'Observable' represents a 'data stream' and that there can be multiple 'Subscribers' who are interested in consuming this asynchronous data stream.
  27.  
  28.  
  29. A data stream, as I'm defining it, is just ordered data that has a well-defined stopping point and a way of notifying processors of the data that an error has occurred.
  30. Java’s Reader would count as a data stream in the sense that I’m defining it here because its read() method returns -1 once its reached the end of a byte[] and because it throws exceptions if there’s an error with processing the data.
  31.  
  32.  
  33. Functionally Transformed - no side effects, you don't modify data outside the function. you just operate on what has happened.
  34. Example Psuedo code: "
  35.  
  36.  
  37. Video: https://www.youtube.com/watch?t=2601&v=Qi18tLf_eyI
  38. Slide: https://prezi.com/xby6mpyb_4uo/rxjava/?utm_campaign=share&utm_medium=copy
  39. Post: Part 1: http://www.philosophicalhacker.com/2015/06/12/an-introduction-to-rxjava-for-android/ Part 2: http://www.philosophicalhacker.com/2015/06/19/introduction-to-rxjava-for-android-pt-2/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement