I recently enhanced Stamp Pack to use a dynamic backend in Firebase and local caching with Realm. This was my first time trying out Realm after hearing nothing but great things about it for years. This post is a summary of my experience and why I chose Realm instead of Core Data, which has been my go-to persistence framework.
My Bias: All Apple, All Day, Every Day
I’m super duper heavily biased towards Apple. I prefer first-party frameworks and only explore third parties after exhausting first-party options.
The Task: Update an existing local cache with new data obtained on a background thread.
Recently when I was enhancing Stamp Pack, I came across a common situation. I’m updating stamps on Firebase on the fly and I want the local cache and UI in the iMessage app to reflect these changes as quickly as possible. The notification from Firebase to the mobile device is very fast. We’re talking 90ms fast. At that point, it’s up to the local persistence to be updated with the new data and then notify the UI of the changes.
Using Core Data
Here are the steps required to accomplish this task in Core Data
1) Create a model using the GUI
2) Create an entity for the model in step 1 (semi — optional)
3) Create a Persistence Store Container
4) When the notification comes in on a background thread, create a background managed object context.
5) Assign a merge policy
6) Create a new instance of our entity.
7) Assign the new instance the properties from the update
8) Save the entity
Here are the steps required to accomplish this task in Realm
- Create a model
2) When the notification comes in on a background thread, create a new Realm.
3) Create a new instance of our object.
4) Assign the new instance the properties from the update.
5) Save the object.
The Task: Query the data for a particular record
In addition to adding and updating data, we also need to query data. Let’s look at the differences here.
The Task: Query the data for a record containing a substring
Let’s broaden the query to search for a substring.
Core Data setup is a more lengthy process than Realm with almost double the work. The Core Data code I’m showing here is rather new and only available in iOS 10 and later. In years past, there would be 3–5 times more code involved in this setup. Even after all of the Core Data improvements in recent years, Realm is simpler to setup and the code for Realm is easier to read. I cheated a little for brevity by not handling Realm errors but wrapping Core Data in do-catch blocks. The truth is I am more afraid of Core Data blowing up than Realm, but errors should be handled in Realm as well.
Thanks for reading. This was an expanded discussion on some of the frameworks I used in my blog post How to pay for a MacBook Pro using only Stickers. Speaking of which, I’m still working on getting that MacBook Pro, so if you’d like to support me in that effort you can help by:
1) Purchasing Stamp Pack. I promise it’s great and you’ll have a lot of fun using it.
2) Sharing This Post.
3) Random Acts of Kindness. I prefer the first two, but if you want, feel free to donate to the cause via PayPal.