本文介绍了Realm 对象上的脏标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能提出一种在 Realm 对象上实现脏标志的好模式?具体来说,我希望 Realm Object 的每个子类都公开一个 isDirty 标志,该标志在类的实例被修改时设置并在实例写入云(而不是 Realm)时清除.我在使用 Objective-C.

Can anyone suggest a good pattern for implementing a dirty flag on Realm objects? Specifically, I would like every subclass of Realm Object to expose an isDirty flag that gets set whenever an instance of the class is modified and is cleared whenever the instance is written to the cloud (not the Realm). I'm working in Objective-C.

我能想到的可能解决方案包括:

Possible solutions I can think of include the following:

  • 为每个对象的每个属性编写一个自定义的 setter.在每个 setter 中设置 isDirty.不太理想.
  • 以某种方式使用 KVO.这种方法有两个问题:(a) 我不完全理解如何实现这种方法,以及 (b) Realm 不支持托管对象的 KVO(这正是我需要它的对象).
  • 使用领域通知.同样,我没有这些经验,我不确定如何将它们用于此目的.

推荐答案

除了在执行每个写入事务后手动设置非托管的 isDirty 属性外,KVO 将是最好的方法去.

Short of simply having a non-managed isDirty property that you manually set after performing each write transaction, KVO would be the best way to go.

设置自定义 setter 确实会非常混乱.您必须为要跟踪的每个属性设置一个单独的属性.

Setting custom setters would indeed be incredibly messy. You'd have to have a separate one for each property you wanted to track.

领域通知仅在您跟踪一组对象并希望在任何对象发生更改时收到警报时才有效(使用 收集通知) 或 如果有领域改变了.

Realm notifications would only work if you were tracking a set of objects and wanted to be alerted if any were changed (using collection notifications) or if anything in the Realm changed.

使用 KVO,您可以潜在地让您的对象子类本身为它的所有属性添加观察者,然后当它们中的任何一个发生变化时,这些观察者就会被引导到一个方法,然后这可以用来标记 isDirty 属性.

With KVO, you could potentially get your object subclass itself to add observers to all of its properties, which are then channeled to one method whenever any of them change, this could then be used to mark the isDirty property.

@interface MyObject: RLMObject

@property NSString *name;
@property NSInteger age;

@property BOOL isDirty;

- (void)startObserving;
- (void)stopObserving;

@end

@implementation MyObject

- (void)startObserving
{
    NSArray *properties = self.objectSchema.properties;
    for (RLMProperty *property in properties) {
       [self addObserver:self forKeyPath:property.name options:NSKeyValueObservingOptionNew context:nil];
    }
}

- (void)stopObserving
{
    NSArray *properties = self.objectSchema.properties;
    for (RLMProperty *property in properties) {
       [self removeObserver:self forKeyPath:property.name];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey,id> *)change
                       context:(void *)context
{
    self.isDirty = YES;
}

+ (NSArray *)ignoredProperties {
    return @[@"isDirty"];
}

@end

显然,您希望在这里进行比我所做的更多的检查(以确保确实需要设置 isDirty),但这应该会给您一个想法.

Obviously you'd want to do more checking in here than I've done (to make sure isDirty truly needs to be set), but this should give you an idea.

没有真正的方法可以自动知道何时创建了托管 Realm 对象,因此您最好根据需要手动启动和停止观察.

There's no real way to automatically know when a managed Realm object has been created, so it would be best for you to manually start and stop observing as you need it.

这篇关于Realm 对象上的脏标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:52