【发布时间】:2020-02-26 21:27:19
【问题描述】:
我想通过指定事件过滤Observable集合内的List <Notification>。
这是一个改造电话:
@GET("/notifications")
Observable<NotificationCollection> getNotifications(@Query("page") Integer page);
NotificationCollection模特:
class NotificationCollection {
var items: List<Notification>? = null
var pagination: Pagination? = null
}
Notification模特:
class Notification {
var event: String? = null
var id: Long? = null
var data: NotificationData? = null
}
在我的助手类中,我将Observable 返回给交互者:
override fun getNotifications(page: Int): Observable<NotificationCollection> {
return service.getNotifications(page)
}
我尝试了几种方法:
override fun getNotifications(page: Int): Observable<NotificationCollection> {
return service.getNotificationsTimeline(page)
.filter { it.items?.get(0)?.event == "STORY"}
}
在这里,我想将谓词应用于所有列表项,而不仅仅是我通过索引定义的那些。有没有办法制作类似.filter { it.items.event = "STORY"} 的东西?
我在这里尝试使用flatMap 的另一种方法对我来说更有意义,但我看不到如何将过滤后的结果映射到Observable<NotificationCollection> 的原始响应类型而不是像这里的Observable<Notification> :
return service.getNotifications(page)
.flatMap { Observable.fromIterable(it.items) }
.filter {it.event == "STORY"}
最简单的方法是使用flatMap 在我的演示者类中应用filter 函数,但我想概括我的解决方案,因为助手类中的方法在不同的地方被调用。所以我想在这里过滤列表。
那么,有没有办法过滤Observable集合内的列表并返回Observable<NotificationCollection>的原始响应类型?
【问题讨论】: