Kevin Goedecke
Kevin Goedecke

Reputation: 1983

Custom setter/willSet for Realm Object

Whenever I set a property of a Realm object I want to trigger the change of an other object that represents the object on my remote backend.

I was wondering if this is still the best practice recommended:

https://github.com/realm/realm-cocoa/issues/870#issuecomment-54543539

What I was trying to do, but doesn't work because it interferes with Realm:

dynamic var name: String = "" {
    willSet(newValue) {
        self.name = newValue
        self.widgetRemote?.name = newValue
    }
}

Upvotes: 1

Views: 871

Answers (2)

bdash
bdash

Reputation: 18308

Yes, the workaround suggested in realm/realm-cocoa#870 is still the best way to achieve this. For your case you'd want to do something like:

@objc private dynamic var backingName = ""

var name : String {
    get {
        return backingName
    }
    set(newValue) {
        backingName = newValue
        widgetRemote?.name = newValue
    }
}

override class func ignoredProperties() -> [String] {
    return ["name"]
}

Upvotes: 5

Anton Tropashko
Anton Tropashko

Reputation: 5806

bdash's version in objc:

+(NSArray<NSString *> *)ignoredProperties
{
    return @[ @"name" ];
}

see the answer bdash's swiftly provided to obtain more context on backing store exampt from persistence into realm store

Upvotes: 0

Related Questions