Search code examples
androidkotlinrx-java2rx-kotlin

Sorting emissions in Rx using sorted() or toSortedList() not working


I'm trying to sort the emissions of an Observable using either sorted() or toSortedList().

Here's my code:

bookshelfUseCase.addedBooks(uid, BookshelfRepositoryContract.READ_LIST)
            .flatMap { Observable.fromIterable(it) }
            .doOnNext { Log.d("RLP1", it.toString()) }
            .toSortedList { first, second -> first.timestamp.compareTo(second.timestamp) }
            .toObservable()
            .doOnNext { Log.d("RLP2", it.toString()) }
            .subscribe { getView().showBooks(it) }

Here, I'm comparing the timestamp of each Book to get a sorted list for showing it to the user.

What's the problem?

The Observable or Single returned by sorted() or toSortedList() doesn't emit anything. Just blank. Zero emissions.

Here's the output of log printed by the doOnNext() before and after toSortedList() is called:

02-18 01:54:27.743 25032-25032/com.bookwritten D/RLP1: Book(id=368593, title=The 4-Hour Workweek, year=2007, author=Author(id=210456, name=Timothy Ferriss), rating=3.85, coverImageUrl=https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png, coverImageUrlSmall=https://s.gr-assets.com/assets/nophoto/book/50x75-a91bf249278a81aabab721ef782c4a74.png, checkInTime=0, timestamp=1518895501664)
02-18 01:54:27.743 25032-25032/com.bookwritten D/RLP1: Book(id=12605157, title=The $100 Startup: Reinvent the Way You Make a Living, Do What You Love, and Create a New Future, year=2012, author=Author(id=3367145, name=Chris Guillebeau), rating=3.85, coverImageUrl=https://images.gr-assets.com/books/1345666854m/12605157.jpg, coverImageUrlSmall=https://images.gr-assets.com/books/1345666854s/12605157.jpg, checkInTime=0, timestamp=0)
02-18 01:54:27.743 25032-25032/com.bookwritten D/RLP1: Book(id=13497818, title=The Casual Vacancy, year=2012, author=Author(id=1077326, name=J.K. Rowling), rating=3.28, coverImageUrl=https://images.gr-assets.com/books/1509893913m/13497818.jpg, coverImageUrlSmall=https://images.gr-assets.com/books/1509893913s/13497818.jpg, checkInTime=0, timestamp=0)
02-18 01:54:27.743 25032-25032/com.bookwritten D/RLP1: Book(id=29095176, title=Lyrebird, year=2016, author=Author(id=7116, name=Cecelia Ahern), rating=3.76, coverImageUrl=https://images.gr-assets.com/books/1465023152m/29095176.jpg, coverImageUrlSmall=https://images.gr-assets.com/books/1465023152s/29095176.jpg, checkInTime=0, timestamp=0)
02-18 01:54:27.743 25032-25032/com.bookwritten D/RLP1: Book(id=31823677, title=Tools of Titans: The Tactics, Routines, and Habits of Billionaires, Icons, and World-Class Performers, year=2016, author=Author(id=210456, name=Timothy Ferriss), rating=4.25, coverImageUrl=https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png, coverImageUrlSmall=https://s.gr-assets.com/assets/nophoto/book/50x75-a91bf249278a81aabab721ef782c4a74.png, checkInTime=0, timestamp=0)
02-18 01:54:27.743 25032-25032/com.bookwritten D/RLP1: Book(id=36204090, title=Crushing It!: How Great Entrepreneurs Build Their Business and Influence—and How You Can, Too, year=0, author=Author(id=1371305, name=Gary Vaynerchuk), rating=4.58, coverImageUrl=https://images.gr-assets.com/books/1514065832m/36204090.jpg, coverImageUrlSmall=https://images.gr-assets.com/books/1514065832s/36204090.jpg, checkInTime=0, timestamp=0)

As you can see, no logs are being printed after the toSortedList() has been called in the chain.

What could I be missing here?

Any help is highly appreciated. Thanks in advance.


Solution

  • Looks like I made the mistake of applying sorted() on an infinite Observable, as pointed out by @akarnokd.

    The Observable in question is infinite because it's connected to a Firebase Realtime DB.

    Therefore, my sorting problem was easily solved by sorting the values on subscribe{} rather than applying the function within the chain. The code goes like this:

    bookshelfUseCase.addedBooks(uid, BookshelfRepositoryContract.MY_BOOKS)
                .subscribe { getView().showBooks(it.sortedWith(compareByDescending { it.timestamp })) }
    

    Hope this saves someone's time.