Realm

Every one of us has found himself in a situation where we couldn’t connect to the internet. The reason for that could be that we spent all of the data in our data plan or just simply that there is no cell coverage in a specific area. At that point we are left to the mercy of developers — does the app work without an internet connection or are we stuck with the loading indicator. Every decent app should and must be able to work offline because otherwise — it just plain sucks! A technical term for that is persisting data.

Realm was designed to be faster and more efficient than the previous database solutions. This new solution is a cross-platform mobile database called Realm. It is available in Objective-C and Swift, and it’s designed for iOS and Android.

The major upsides of Realm are:

  • It’s absolutely free of charge.
  • Fast, and easy to use.
  • Unlimited use.
  • Work on its own persistence engine for speed and performance.

What’s really great about it is that you can handle all the work with a couple of lines of code. Realm is very easy to install and faster to work with compared to SQLite and Core Data. Also, the database files are shareable among iOS and Android.

If you are designing an app with a lot of records and for a large number of users, you need to pay special attention to scalability from the very beginning. Realm is great at this and it allows you to handle a lot of data fast.

To get started with Realm, all you need is at least iOS 8 or OS X 10.9. Older versions don’t support this new straightforward solution for managing local storage and databases.

Switching To Realm

If you have been working with Core Data and you want to move on to Realm, the process is straightforward. Many developers have made the journey in a couple hours. Note that both Core Data and Realm treat data as objects so what you need to do is refactor the Core Data code to use Realm.

Realm has outline the process thoroughly, but here is a summary:

  • Remove the Core Data Framework. Locate the part of your code that include Core Data code and refactor them.  They outline a nice trick to throw a compiler error for each line of code using Core Data.
  • Remove the Core Data Setup Code. There’s going to be a portion of Core Data setup code somewhere in your app and you need to get rid of it all. Realm is automatically configured as soon as you access a Realm object for the first time. You can choose where you want to store the Realm data file, and it will still be optional at runtime.
  • Migrate Your Model Files. You can easily convert managed object subclasses to Realm. Realm manages property keywords internally (you don’t have to specify them), which makes the class header look more minimal. Also, you can safely remove all NSNumber cruft because Realm supports simple numerical data (NSInteger and CGFloat). There are also limitations to Realm. Unlike Core Data whose objects have NSManagedObjectID to identify objects uniquely, Realm leaves this to you as the developer. Secondly, the current version of Realm can’t deal with object properties with a nil value. This is a minor inconvenience, but the developers are promising that the next version of Realm won’t have this problem.
  • Migrating Your Write Operations. Realm’s save operations are a little different than in Core Data and you need to get familiar with this. Once the Realm objects are added to a Realm object, they can’t be modified. This ensures consistency of data in different threads. To be able to modify the properties, the object where these were saved needs to be in a ‘write’ transaction.
  • Migrate Your Queries to be able to retrieve your data as needed. To fetch a file, in Core Data you need approximately 10 lines of code. In Realm, you can do the same thing with just one line.
  • Migrate Your Users’ Production Data for active apps created in Core Data, you can re-link the Core Data framework back into the app, fetch user’s data and pass it on to Realm. An easier solution for replaceable users’ data is to remove all Core Data save files and start over the next time the app is open.

The key is to keep your design process as simple with the best techniques and tools. Once you’ve invested time to set up your tried and true process don’t let the “attraction to new” take you off course. May “the new” work to be a part of your process and time is the detector of that is not worthy.   

 

Major Classes

In order to better understand what Realm does, here’s an overview of the Realm classes and concepts you’ll use in this tutorial:

  • Realm: Realm instances are the heart of the framework; it’s your access point to the underlying database, similar to a Core Data managed object context. You will create instances using the Realm() initializer.
  • Object: This is your Realm model. The act of creating a model defines the schema of the database; to create a model you simply subclass Object and define the fields you want to persist as properties.
  • Relationships: You create one-to-many relationships between objects by simply declaring a property of the type of the Object you want to refer to. You can create many-to-one and many-to-many relationships via a property of type List.
  • Write Transactions: Any operations in the database such as creating, editing, or deleting objects must be performed within writes which are done by calling write(_:) on Realm instances.
  • Queries: To retrieve objects from the database you’ll need to use queries. The simplest form of a query is calling objects() on a Realm instance, passing in the class of the Object you are looking for. If your data retrieval needs are more complex you can make use of predicates, chain your queries, and order your results as well.
  • Results: Results is an auto updating container type that you get back from object queries. They have a lot of similarities with regular Arrays, including the subscript syntax for grabbing an item at an index.