我尝试使用Observable
或sorted()
对toSortedList()
的排放进行排序。
这是我的代码:
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
的时间戳进行比较,以获取排序列表,以便向用户展示。
Observable
或Single
返回的sorted()
或toSortedList()
不会发出任何内容。只是空白。零排放。
此处调用doOnNext()
之前和之后toSortedList()
打印的日志输出:
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)
如您所见,在链中调用toSortedList()
后没有打印日志。
我可以在这里找到什么?
非常感谢任何帮助。提前谢谢。
答案 0 :(得分:0)
正如@akarnokd所指出的那样,我似乎错误地将sorted()
应用于无限Observable
。
有问题的Observable
是无限的,因为它已连接到 Firebase实时数据库。
因此,我可以通过对subscribe{}
上的值进行排序而不是在链中应用函数来轻松解决排序问题。代码如下:
bookshelfUseCase.addedBooks(uid, BookshelfRepositoryContract.MY_BOOKS)
.subscribe { getView().showBooks(it.sortedWith(compareByDescending { it.timestamp })) }
希望这能节省一些人的时间。