RxSwift驱动程序值的变异
在我的应用程序中,我有一系列通知。通知可以读也可以不读 当用户单击未读通知时,我需要更改模型并在表视图中重新加载数据 在我的ViewModel中,我有输出流:RxSwift驱动程序值的变异,swift,cocoa-touch,rx-swift,Swift,Cocoa Touch,Rx Swift,在我的应用程序中,我有一系列通知。通知可以读也可以不读 当用户单击未读通知时,我需要更改模型并在表视图中重新加载数据 在我的ViewModel中,我有输出流: let notifications: Driver<[Notification]> 你有什么办法让它变的吗?谢谢。默认情况下允许使用onNext的参数。您可以使用var定义一个新的变量,即“var newNotification=notification”,然后在修改后返回它。流是不可变的(这与可观察的,驱动程序和任何其他特
let notifications: Driver<[Notification]>
你有什么办法让它变的吗?谢谢。默认情况下允许使用onNext的参数。您可以使用var定义一个新的变量,即“var newNotification=notification”,然后在修改后返回它。流是不可变的(这与
可观察的,驱动程序和任何其他特征相同)。它们是“只读”的,您可以随时间从流中读取值
一般来说,观测量值有一个“值”的概念有点错误,因为观测量值代表一个随时间变化的值,而不仅仅是一个值
在构建驱动程序时,您要做的是“考虑”您的PublishSubject
类似这样的方法会奏效:
notifications = Observable
.combineLatest(touchedNotification, readNotification, otherEvent) { ($0, $1, $2) }
.map { ... map the three values into whatever makes sense for you }
.asDriver(onErrorJustReturn: ... fallback value ... }
同样,要记住的最重要的事实是,您实际上并没有对流进行变异,您只是将它们组合起来,进行转换,等等,以创建一个适合您需要的新流
希望这对你有帮助 我猜通知
是一种值类型,类似于结构
。变异通知的单个值
是没有意义的,因为您正在创建一个全新的通知
。您可以尝试将通知
改为类
,这是一种引用类型。@daltonclaybrook您是对的,它是struct,但如果我将其更改为类,则会影响项目架构。所以我只是想用rx方法找到一个解决方案。您好,这无助于解决问题,因为在我的输出流中,是为表视图绑定的,我需要其中的mutate对象。您的意思是,您正在尝试修改别处定义的通知对象?然后,您可以通过self.notification或任何定义的地方直接引用它。没有更多的上下文很难说。
touchSingleNotificationIntent
.filter { !$0.isRead }
.do(onNext: { notification in
notification.isRead = true // I need to change state of the model immediately after user iteration
})
.map { $0.notificationID }
.flatMap(markNotificationAsRead) // http request which doesn't reply with current notification model status
.subscribe()
.disposed(by: bag)
notifications = Observable
.combineLatest(touchedNotification, readNotification, otherEvent) { ($0, $1, $2) }
.map { ... map the three values into whatever makes sense for you }
.asDriver(onErrorJustReturn: ... fallback value ... }