使用 sorted() 或 toSortedList() 对 Rx 中的排放进行排序不起作用

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

我正在尝试使用 sorted()toSortedList()Observable 的排放量进行排序。

这是我的代码:

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) }

在这里,我正在比较每个 Book 的时间戳以获得一个排序列表以显示给用户。

有什么问题?

sorted()toSortedList() 返回的 ObservableSingle 不会发出任何内容。只是空白。零排放。

调用toSortedList()前后doOnNext()打印的日志输出如下:

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 0 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)

如您所见,在链中调用 toSortedList() 后没有打印任何日志。

我可能在这里遗漏了什么?

非常感谢任何帮助。提前致谢。

看来我错误地将 sorted() 应用于无限 Observable,正如@akarnokd 所指出的。

问题中的 Observable 是无限的,因为它连接到 Firebase 实时数据库

因此,我的排序问题很容易通过对 subscribe{} 上的值进行排序而不是在链中应用函数来解决。代码如下:

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

希望这可以节省一些人的时间。