YouTube Videos

A Simple Neural Network
KotlinConf 2018 - Mathematical Modeling
Creating a Sudoku Solver from Scratch
Traveling Salesman Problem
Text Categorization w/ Naive Bayes
Monty Hall Problem
Solving World's Hardest Sudoku

Wednesday, November 18, 2015

RxJava Operators - toList()

If you read my earlier post Understanding Observables, you already know that I like to think of Observable operations as stick figures passing boxes. The analogy made sense to me and it seemed to help others too. As I get more efficient at cheap animation, I'll see if I can cover other operators with these "RxMovers". I even have some ideas to visualize subscribeOn() and observeOn() with these guys.

The toList() operation reveals quite a bit about Observable behavior and how onNext() and onCompleted() calls can be manipulated. In my previous article that covered map(), the onNext() calls happen in sequence down the entire chain and get passed all the way to the final Subscriber.

The toList() operator intercepts onNext() calls. Rather than handing the items further down the chain, it catches them instead and adds them to a List (specifically an ArrayList). Only when the source Observable calls onCompleted() will it emit the entire List up the chain, then it will call onCompleted() up the chain.


The toList() in Action




The scene starts by Mover 1 taking boxes out of storage and pushing them to Mover 2, who represents the toList() operation. Instead of passing the boxes immediately to Mover 3, he instead collects them onto a dolly and does not pass anything to Mover 3. When Mover 1 has passed all items from the storage unit (which is three items), he calls onCompleted() on Mover 2 and says they are done.

However, Mover 2 does not pass the onCompleted() call to Mover 3 yet. This is his cue to push the dolly of items to Mover 3. He calls Mover 3's onNext() method and passes the entire List<Box> of items to him. Then Mover 2 calls onCompleted() which signals Mover 3 to close the truck.

This is how it may look in RxJava.

Observable<Box> mover1 = Observable.create(s -> {
 while (house.hasItems()) {
  s.onNext(house.getItem());
 }
 s.onCompleted();
});

Observable<List<Box>> mover2 = mover1.toList();

Subscription mover3 = mover2.subscribe(listOfBoxes -> 
    putInTruck(listOfBoxes));


The importance of onCompleted()

As you can probably guess, the onCompleted() call is very important with an operation like toList(). Logically, you cannot collect infinite emissions from an infinite Observable into a List, because a List is a finite collection of items. Therefore, if an onCompleted() call is never passed to a toList() operation, you will get some very undesired behaviors. These behaviors can range from infinite blocking because toList() infinitely keeps waiting for items, or it gives up and does not emit anything at all.

A few other operators are dependent on the onCompleted() call, such as last() and count(). So always be mindful and ensure the sequences are finite. Let me know if you have any questions.

Be sure to read my latest RxMover post on flatMap().

40 comments:

  1. Very good metaphor. I like the idea of animation! It is a very good tool, however, how much time did it take for you to create this animation?

    ReplyDelete
    Replies
    1. Glad you like it! With the learning curve with GIMP and Inkscape it took me about 90 minutes. But now that I understand layers, sprite, and scene reusability better, I think I can compose a scene in about 15-20 minutes. I plan on making more : )

      Delete
    2. GIMP made composing GIFs surprisingly doable. The tricky part for me was understanding and leveraging layers to maximize re-usability. But here was a helpful simple tutorial I used https://www.youtube.com/watch?v=f-9_HwRcd1A

      Delete
  2. You and your cool animations are doing this magic, I never understand RxJava this clear with this short explanation. Thank you :)

    ReplyDelete
  3. I am glad you are finding them helpful :)

    ReplyDelete
  4. very nice ,cool and easy explanation of RxJava concepts i ever come across. Thank you , Thomas. Waiting for your articles on other RxJava concepts

    ReplyDelete
    Replies
    1. Thank you! More articles will come. For now I'm writing an open book "Learning RxJava with JavaFX", as well as prepping for an O'Reilly webinar. But I've got some future article ideas that I will jot down soon...

      Delete
  5. geek squad chat with an agent issues with your cellphone? Unable to install the software application on your PC? From cellphone to camcorder the certified experts have solutions for every issue. Find proper fixes to issues to every hardware related issue you face. Seek the specialized help at Geek Squad Tech Support for all your device related issues. Best tech help is available in-store, at home and at your workplace.geek squad chat Don’t get bothered with issues affecting your device performance. Discuss the issues with Geek Squad Agents and get proper fixes for issues. The certified agents help you resolve your issues instantly. Connect with Professionals round the clock anytime, anywhere according to your comfort. geek squad chat

    ReplyDelete

  6. 192.168.0.1

    192.168.l0.1 Router login

    https://www-1921681254.com This IP address is used by the routers like TP-Link, Netgear, D-Link uses it as the default IP address.


    ReplyDelete
  7. Great share!

    Loved all that you shared, and you are right, one cannot deny the power of Google. I simply love it and am part of a number of
    communities too. The point you mention in your post Sand blasting machine works that are very useful for me. I understand the way of the attractive to the customer with the products.

    Keep it works and share with us your latest information. For more Information or any help then visit now at Sand Blasting Machine, Sand Blasting Machine for Sale, Sand Blasting Machine Manufacturer, and Small Sand Blasting Machine.

    They are similar to small communities that you own - check them out if you haven't already.It's all got a lot better than before!t.

    Thanks for sharing. Have a nice week ahead.

    Visit at :www.blaster.co.in

    Regards,
    Purnima Sharma.

    ReplyDelete
  8. quickbooks error 15311 is one of the common error we might have face while working on Quickbooks ,here we have discussed some of measures through which you can fix these errors.

    ReplyDelete

  9. This is default IP address that is used by most of the router companies like the PTCL, TP-Link, D-Link, in order to access the admin page of the router

    192.168.10.1

    192-168-l0-1.club

    ReplyDelete

  10. This configuration can also be used for devices like Router, Modem etc. and they are all having the initial IP address

    192.168.l0.1

    192.168.0.1

    ReplyDelete
  11. I like the helpful info you provide in your articles. I’ll bookmark your blog and check again here frequently. I’m quite sure I’ll learn plenty of new stuff right here! Good luck for the next.

    ReplyDelete
  12. This post is disseminating valuable information to people who are most concerned of the following issues being targeted by this site. Many certainly will keep coming back to check out updated posts.
    고스톱

    ReplyDelete
  13. Only wanna input on few general things, The website layout is perfect, the written content is rattling superb.
    스포츠토토

    ReplyDelete
  14. You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...
    일본야동

    ReplyDelete
  15. An impressive share, I just now with all this onto a colleague who was conducting a little analysis during this.
    토토사이트

    ReplyDelete
  16. Thanks For this great content. Really Enjoyed.Keep It up. 스포츠토토

    ReplyDelete
  17. Nice info, I love the way you express words with emotions. 온라인경마

    ReplyDelete
  18. Can I simply say what a relief to find somebody who really understands what they are discussing on the web.
    스포츠토토핫

    ReplyDelete
  19. I’m really enjoying the design and layout of your website.
    바카라사이트윈

    ReplyDelete
  20. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. 먹튀검증 and I am very happy to see your post just in time and it was a great help. Thank you ! Leave your blog address below. Please visit me anytime!

    ReplyDelete
  21. As I am looking at your writing, slotsite I regret being unable to do outdoor activities due to Corona 19, and I miss my old daily life. If you also miss the daily life of those days, would you please visit my site once? My site is a site where I post about photos and daily life when I was free.

    ReplyDelete
  22. Kaufen Sie den Führerschein in Frankreich ohne Prüfung. Wir bieten absolut alle Führerscheinklassen in Deutschland, Österreich und der Schweiz an.

    Zweiräder der Klasse A in Unterklassen der Klasse D für Personenkraftwagen, einschließlich der Unterklassen B.

    und C für leichte Fahrzeuge und LKW. Weitere Informationen zu den Führerscheinklassen finden Sie unter
    führerschein kaufen legal, führerschein kaufen ohne vorkasse, registrierten führerschein kaufen erfahrungen, führerschein kaufen erfahrungen, führerschein kaufen ohne prüfung Köln, führerschein kaufen österreich, führerschein kaufen ohne prüfung österreich, führerschein kaufen ohne prüfung österreich, führerschein kaufen in österreich, führerschein kaufen Frankfurt, führerschein kaufen schweiz

    Wir produzieren deutschen Führerschein ohne - EU MPU Führerschein. Wir entsperren Sie

    Im System. Wir speichern Ihre Daten in der Datenbank.

    kjop-forerkort-uten-eksamen.

    comprare-carta-conducao.

    f-a-qs.

    comprare-la-patente.

    echten-fuhrerschein-kaufen.

    f-a-qs.

    f-a-qs.

    fin.

    kontakt-oss.

    ReplyDelete