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

  1. Create an object.
  2. Assign a non-nil value to object’s attribute.
  3. Save.
  4. Create another object with same primary key.
  5. Assign a nil value to the same object’s attribute.
  6. Save.
  7. Fetch.
  8. 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

Most upvoted comments

@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…