Effect of Deleting an Object in a Map?


Suppose I have this:

final class Foo: Object
    @Persisted var kids: Map<String, Bar?>

final class Bar: Object

And then I take a Bar object that is part of a Realm and I do this:

try someRealm.write {


What becomes of the kids map on the Foo parent? Is the Key removed from the map entirely (like if kids were a List)? Or does the key remain in the map and simply have a nil value now?

It would be nice if the docs explicitly described this. The presence of an Optional in the value part of Map makes it unclear what happens on deletion when the value is an Object subclass.

Great question!

Two things we see when working with Maps:

  1. Unrelated to the question: If the Map is set to nil, the entire map entry is removed. So if this is done

f.kids["some_key"] = bar

and then later this is done

f.kids["some_key"] = nil

the entire map entry is removed.

  1. For your question; If the value part points to another object. If that object is deleted, the map entry remains but the value is nil

f.kids["some_key"] = bar

and then bar is deleted


the map remains, and the value is nil

If you were to iterate over the kids map and the bar object was deleted

for map in foo.kids {
   print(map.key, map.value)

it will output

some_key nil

And yes! I agree - the documentation on Map is a bit “thin” to begin with. I suggest heading over to that documentation page and in the lower right corner, click the Share Feedback button and make a suggestion that the map section needs some love and additional/clear examples.

“To remove a Map, set it to nil etc etc. Deleting an object a map points too, leaves the map in place with a value of nil.”

Thanks Jay. Please see this discussion: Docs: Specify Behavior of Map when an Object value is deleted · Issue #8379 · realm/realm-swift · GitHub

It appears that Map’s current behavior (leaving the key in-place when an object is deleted from the Realm) will result in data-loss when using Atlas Device Sync. Atlas does NOT keep the [someKey: nil] record on the MongoDB document. The web interface shows the property backing the Map as completely empty.

If I deleted the local Realm file and re-synced from the cloud, I’d get back an empty Map, not my Map with [someKey: nil]

I also can’t tell why Map behaves this way. If I directly set a value to nil, the associated key is nuked. But if the value gets set to nil indirectly (the object is deleted from Realm), the key remains with a nil value (but this state can’t be synced to Atlas)…it seems like an incongruent mess.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.