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).

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store