realm-swift: Cannot nil a property on an object by updating with a nil value.
Goals
Able to save nil attributes.
Expected Results
Existing attribute with non-nil value will become nil.
Actual Results
Could not overwrite existing value with nil value. The existing value is always returned.
Steps to Reproduce
- Create an object.
- Assign a non-nil value to object’s attribute.
- Save.
- Create another object with same primary key.
- Assign a nil value to the same object’s attribute.
- Save.
- Fetch.
- Notice that the attribute is not nil.
Code Sample
class RealmObject: Object {
dynamic var id = 0
dynamic var attribute: String?
override static func primaryKey() -> String? {
return "id"
}
}
let realmObject = RealmObject()
realmObject.attribute = "My Attribute"
try! Realm().add(realmObject, update: true)
let realmObject = RealmObject()
realmObject.attribute = nil
try! Realm().add(realmObject, update: true)
// At this point, realmObject.attribute is still equal to "My Attribute."
Version of Realm and Tooling
Realm framework version: 2.8.0
Realm Object Server version: ?
Xcode version: 8.3.2
iOS/OSX version: 10.3
Dependency manager + version: Cocoapods 1.2.0
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 8
- Comments: 17 (10 by maintainers)
Commits related to this issue
- Set properties to `nil` when addOrUpdating a RLMObject The hack to preserve existing behavior for createOrUpdate was overly broad and also affected addOrUpdate. The newly added test passes in 2.7.0 a... — committed to realm/realm-swift by tgoyne 7 years ago
- Set properties to `nil` when addOrUpdating a RLMObject The hack to preserve existing behavior for createOrUpdate was overly broad and also affected addOrUpdate. The newly added test passes in 2.7.0 a... — committed to realm/realm-swift by tgoyne 7 years ago
- Fix add(update: true) for nil RealmOptional properties This also needs the `?: NSNull.null` so that existing values are actually set to null, along with the hack to not do this for create() until 3.0... — committed to realm/realm-swift by tgoyne 7 years ago
- Fix add(update: true) for nil RealmOptional properties This also needs the `?: NSNull.null` so that existing values are actually set to null, along with the hack to not do this for create() until 3.0... — committed to realm/realm-swift by tgoyne 7 years ago
- Fix add(update: true) for nil RealmOptional properties This also needs the `?: NSNull.null` so that existing values are actually set to null, along with the hack to not do this for create() until 3.0... — committed to realm/realm-swift by tgoyne 7 years ago
@austinzheng is this resolved in
3.0.0?Can I confirm how one might distinguish between setting a property to nil and not touching a property at all when updating an existing object in store? As I’d like both behaviour when using optionals…