ios - 用 ReactiveCocoa 观察 Realm 的属性对象,当对象失效时如何处理

标签 ios objective-c mvvm realm reactive-cocoa

我正在尝试实现一个 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/

相关文章:

android - 电晕函数和变量

ios - AVSpeechSynthesizer didFinishSpeechUtterance 未被调用

ios - Spritekit Swift 游戏菜单放大屏幕

iphone - 按 ID 加载单个视频的 YouTube GData 提要

wpf - WPF 中用于绑定(bind)的嵌套列表的建议方法

silverlight 资源与 VS 设计器不兼容?

android - 在android中实现MVVM时对网络层的错误处理感到困惑,如何通知用户有问题?

ios - iOS 7 上的 UIVibrancyEffect

iphone - 第一次向 NSMutableArray 添加对象

objective-c - MVC 与 KVO 的实现