iOS Database Comparison: SQLLite vs. Core Data vs. Realm
iOS Database Comparison: SQLLite vs. Core Data vs. Realm
This article covers the pros and cons of the major iOS database options and discusses the process of switching to Realm if you are already using SQLite or Core Data. Read on and learn more.
Join the DZone community and get the full member experience.Join For Free
Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%!
If you want to make a great app that runs fast and just works (no bugs allowed) you’ll have to think about all development aspects of your app carefully. One of the aspects you must figure out is how to store and search for large amounts of data, so you’ll probably use a database. The most common options for iOS databases are SQLite and Core Data, plus a relatively newer player called Realm.
This article covers the pros and cons of each option and discusses the process of switching to Realm if you are already using SQLite or Core Data.
SQLite is the most used database engine in the world and its open source as well. It implements a transactional SQL database engine with no configuration and no server required. SQLite is accessible on Mac OS-X, iOS, Android, Linux, and Windows.
It delivers a simple and user-friendly programming interface as it is written in ANSI-C. SQLite is also very small and light and the complete database can be stored in one cross-platform disk file.
The reasons for the great popularity of SQLite are its:
- Independence from a server
- Safe access from multiple processes and threads
- Stores data in tables with one or more columns that contain a specific type of data
Core Data is the second main iOS storage technology available to app developers. Depending on the type of data and the amount of data you need to manage and store, both SQLite and Core Data have their pros and cons. Core Data focuses more on objects than the traditional table database methods. With Core Data, you are actually storing contents of an object which is represented by a class in Objective-C.
Although they are fundamentally different, Core data:
- Uses more memory than SQLite
- Uses more storage space than SQLite
- Faster in fetching records than SQLite
There’s a new(ish) player in town called Realm. 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
- Works 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 outlined the process thoroughly, but here is a summary:
- Remove the Core Data Framework. Locate the parts 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 up 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 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 let time decide what is not worthy.
Published at DZone with permission of Alexander Prusak . See the original article here.
Opinions expressed by DZone contributors are their own.