Jon Vogel
1 min readApr 23, 2019

--

Great article! Just want to point out an issue someone might come across when using Realm.add(<object>, update: true) . If you decode a JSON object that does not have all the properties in the JSON you end up with a realm object with some properties as nil. If you update the object in realm this way you end up setting those properties to nil. For example, let’s say you had an update to the first user in the example JSON. His username was updated so you receive a push like this:

{"id": "e52f2d84-cc00-11e8-9dec-784f435e4a9a", "userName": "Adib Contractor"}

Updating the Realm object using the described method will first throw an error when trying to decode the playlists. Fix the error by using decodeIfPresent . But updating an object with a nil property will set that property to nil. A Realm has a convenience method to work around this where you can pass a dictionary of values you want to be updated. At this point is Decodable really saving us any time? Maybe saving potential errors. This situation would be more common if using GraphQL as it encourages querying just what you need (omitting properties).

--

--

Jon Vogel

I contribute to the start-up grind in Seattle as an iOS Engineer. I also used to fly airplanes.