Reputation: 195
I have a Realm object that may or may not have been added to Realm yet, and I want to update a property (while adding it if it hasn't been added yet).
Here's how I'm doing this right now:
try! realm.write {
if cat.realm == nil {
realm.add(cat)
}
cat.name = "Photon"
}
Is checking if the object has a realm attached to it the correct way to determine if it's been added or not?
Upvotes: 0
Views: 943
Reputation: 15991
Yes. Checking Object.realm == nil
is the best way to see if an object has already been inserted into a Realm or not yet. :)
Upvotes: 1