I read many times “Realm” word on the internet. I even had opportunity to attend one of Realm’s Swift Language User Group meetups in October. Finally, I found free time to use their framework.
Currently I am on a diet. I need to loose some weight because I put on a bit there in California - so much good food :) I browsed the iTunes Store looking for an app that tracks water intake, and found few that IMO were looking bad or UX was poor. I thought, if I decide to create such app for me I’ll kill two birds with one stone - I’ll write the app for me that look like I want to, and I’ll use Realm framework instead of Core Data. So I did.
My first thoughts after I went through a documentation and started using Realm framework? Wow, this is damn good. Guys did a great job!
Disclaimer: The following text covers just a bit of Realm framework. It shows the basics. I recommend to go and read documentation for more details. I didn’t want to show you everything about Realm here, because the post would be as long as the documentation is, and I wanted to avoid this. The documentation is really good. You’ll have to read it too before start working on it.
My use case wasn’t complicated, was rather simple. The app has only two model
DrinkLogEntry. Besides, I needed also functionalities like:
creating, updating, filtering and sorting stored data - As I mentioned, simple app.
Below I’ll present some snippets from the app I created.
There is no xcdatamodel-like file for Realm. The model are just files with
classes that inherits from
Any properties that have dynamic keyword will be parsed and will be part of a data model. Realm also supports relationships. In example the Day class has one-to-many relationship called drinkLogs. One-to-one relationship is just a property with proper type.
Realm also supports migration between versions like Core Data. You can define a block that will be executed when migration is needed, and you can perform all the actions you need to properly go through migration.
Indexed properties and primary keys
The Realm framework has nice features that Core Data does not have (or I couldn’t find them, or I just wanted to mention about them :) ). The first one are indexed properties. You can declare array of properties that are indexed. So searching should be faster with smaller number of properties. This is good for performance.
Next thing are primary keys. You can declare which property is a primary key for the model object. The key is used for updating more efficiently and enforces uniqueness for each value.
In my use case both primary key and indexed property is “identifier” which I used for searching and updating objects.
The object can have also ignored properties, that will not be persistent.
Creating, updating and writing
You can use model object as is without persisting them and write them into Realm store after the object is ready for that. This is the thing that I like more than Core Data solution to play with temporary child context.
To write to Realm or to read from you have to create
This is how you can add new object to database.
I like the way how objects can be updated. Let’s pretend that some object is downloaded from the internet, it is mapped and added to the database. An existing object in database should be updated instead of adding new if there is any.
This is a way better than manually performing queries on context and looking for objects with the same identifier and then updating fields.
If update parameter will be false, and new object will have the same primary key as existing object, the exception will be thrown.
There are other ways to update objects that I’ll not cover in this article.
This is how you can get all
Filtering is very simple too.
Here are all days sorted ascending by timestamp.
Every time you perform
filter() you’ll get
object that allow you to make additional filtering, sorting, etc - this is powerful
and easy to use.
Realm - would I use it in next app that have even a more complicated model? For sure YES. This is very easy to use framework that can be quickly integrated with the app and provides great, powerful features.
P.S. The app is waiting for review :)
The app landed in the store today - Water Intake - Drink more water and track it daily