我正在尝试实现一个 ViewModel。
我想将 ViewModel 绑定(bind)到模型 (RealmObject)。
我正在使用 Reactive Cocoa (2.5) 将 ViewModel 绑定(bind)到模型。
类似于:
RAC(self, name) = RACObserve(self, boundedProfile.name);
RAC(self, pictureUrl) = RACObserve(self, boundedProfile.pictureUrl);
RAC(self, birthday) = RACObserve(self, boundedProfile.birthdate);
我的问题是,如果稍后我删除配置文件并且它变得无效,会发生什么情况?我应该删除所有观察员吗?我如何使用 ReactiveCocoa 做到这一点?
我正在使用 ReactiveCocoa 和 Realm 的 Objective-C 版本。
最佳答案
我认为 RLMObject.invalidated
属性符合 KVO。因此,如果您希望在配置文件失效后中断订阅,您可能只需执行以下操作:
RACSignal *invalidationSignal = [[RACObserve(self, boundedProfile.invalidated)
filter:^BOOL(BOOL invalid) {
return invalid == true; // We're only interested in the cases where it was invalidated.
}]
replayLast]; // For multicasting the same value for all subscribers.
RAC(self, name) = [RACObserve(self, boundedProfile.name)
takeUntil:invalidationSignal]; // Once a value passes here, the subscription breaks.
RAC(self, pictureUrl) = [RACObserve(self, boundedProfile.pictureUrl)
takeUntil:invalidationSignal];
RAC(self, birthday) = [RACObserve(self, boundedProfile.birthdate)
takeUntil:invalidationSignal];
注意事项:
我建议不要直接在 ViewModel 中使用您的数据库模型,而是将它们映射到其他模型仅用于展示,这样处理此类情况会更容易。
关于ios - 用 ReactiveCocoa 观察 Realm 的属性对象,当对象失效时如何处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478109/