Welcome to Innominds Blog
Enjoy our insights and engage with us!

Persistent Storage Options in iOS

By Deepthi Muramshetty,

With the advent of powerful hardwares, capabilities of mobile OS is increasing at a fast pace. In fact, mobiles currently deliver better results than normal computer devices.

With the increasing usability of mobiles, it is important for developers to choose the right persistent storage to manage complex data. A persistent storage is a repository, which stores managed objects.

Database is the most common way to store and manage data. For quite some time, data is handled on server or cloud where mobile device communicate with these databases using API calls. However, to make applications more responsive and less dependent on network connectivity, the trend of offline usage or less dependency on the network is gaining popularity. Nowadays, applications keep data locally or make a copy of it over cloud onto local device and sync it on a timely basis or whenever there is an opportunity depending on the configuration used. It will help in faster and responsive applications, which are functional even when there is no or limited internet connectivity.

Choosing the right database makes a great impact on your application performance apart from responsiveness.

Let's look at some of the most common databases used for persistent storage in iOS applications:

  • SQLite
  • Core Data
  • Firebase
  • Realm

If an application needs to save a small amount of data, there are options available for saving and retrieving the data, namely:

  • NSUserDefaults
  • Plist
  • NSCoder

Database

Let’s evaluate each of the options, and zero-in on the option that suits your requirement in iOS applications.

SQLite

SQ Lite logoSQLite is the most used and open source relational database. It implements a transactional SQL database engine with no configuration. It delivers a simple and user-friendly programming interface as it is written in ANSI-C. It is very small, light and the complete database can be stored in one cross-platform disk file. Below are the pros & cons of SQLite for iOS applications.

Pros:

  • SQLite does not have a separate server process
  • Safe access from multiple processes and threads
  • Stores data in tables with one or more columns that contain a specific type of data
  • Operates on data stored on disk
  • Can drop table and edit data without loading them in memory
  • Database size is small as compared to file based data
  • Simple and powerful queries can be used to add, delete or manipulate data

Cons:

  • Migration process is very long and complex when compared to Core Data
  • Fetching data is a slower process
  • You must have to provide relationships, manually

Core Data

Core Data Core Data is another important storage method for iOS applications. It is a native framework from Apple. It’s a good option for long-term based applications with decent amount of data to be stored. It is fully loaded with features like search, filter, easy migration, etc. Core Data typically decreases by 50% to 70% of the amount of code you write to support the model layer with sophisticated query compilation. Instead of writing SQL queries, you can create complex queries by associating an NSPredicate object with a fetch request. Depending on the type and amount of data one needs to manage and store, both SQLite and Core Data have their pros and cons. Below are the pros & cons of core data for iOS applications.

Pros:

  • Very fast processing to fetch data
  • Graph representation is possible
  • Multithread is possible by using different ’NSManagedObjectContext’
  • Large amount of database can be managed
  • Migration process is simple and easy to add in current source code
  • Apple provides good feature to handle the relationship between two tables

Cons:

  • Consumes more memory & storage space than SQLite
  • Database file is not compatible with any other platform like Android. So, if you are developing an application in iOS and Android, you will have to manage two different databases for both platforms

Firebase

FirebaseFirebase real-time database stores and syncs data with NoSQL cloud database. Data is synced across all clients in real-time and remains available when your app goes offline. It is a cloud-hosted database. Data is stored as JSON and synchronized in real-time to every connected client.

When you build cross-platform apps with firebase iOS, Android, and JavaScript SDKs, all your clients share one real-time database instance and automatically receive updates with the newest data.

Pros:

  • If your app runs on a centralized database and is updated by a lot of users, then it's more than capable of handling the real-time data updates between devices
  • Stored in the cloud; so readily available everywhere
  • JSON (JavaScript Object Notation) storage means no barrier between data and objects
  • Cross-Platform API (If you are using this DB with an app)
  • Hosting is taken care by firebase reducing the hardware maintenance cost
  • Minimal setup, easy access to data, files, auth and more

Cons:

  • Storage format is entirely different from SQL (Firebase uses JSON). Hence, migration presents a difficult challenge
  • Unless the app runs on one centralized database updated by a vast quantity of users, it's an overkill
  • It is limited to 100 connections, 1GB of storage and 10 GB/month download. It also supports more connections and data storage in a subscription model, which is a paid service For more details about pricing, please refer: https://firebase.google.com/pricing/

Reference for Firebase real-time database:
https://firebase.google.com/docs/database/ios/start
Above URL is for installation & setup on iOS

Realm

realm Realm is an open-source object database management system, initially meant for mobile (Android/iOS both objective-c & swift). Now, it is also available for platforms such as Xamarin, React Native and others including desktop applications (Windows) and is licensed under the Apache License. Realm was designed to be faster and more efficient than the previous database solutions.

Realm is easy to install/include in the application and needs minimal configuration. The number of lines needed to accomplish a task/perform a query is very less. It’s a document-based storage system and can be shared between platforms.

Note: Realm is compatible only with versions iOS 8, OS X 10.9, or above. Below are the pros & cons of Realm for iOS applications.

Pros:

  • Performance is faster than SQLite and Core Data
  • Minimal code is required to handle all the work
  • Consistent speed and performance irrespective of large data sets and enormous storage
  • No limit to data storage
  • 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 beginning
  • Realm database files are cross-platform and can be shared among iOS and Android. Regardless of the underlying programming language used like Java, Objective-C, or Swift, one can use their high-level models
  • Scalability is very important while developing your mobile app especially if your app deals with a large number of users and a massive number of records. You should consider that from the beginning while designing and choosing your tools that are to be used
  • Realm is scalable and works with large data in no time. You will bring speed and smoothing to your app while using Realm

Good Documentation & Support: Realm team has provided readable, well organized and rich documentation about Realm. If you have any problems, you can reach them via Twitter, Github or StackOverflow.

Trusted: Realm has been used by many startups and companies in their mobile apps like Pinterest, Dubsmash and Hipmunk.

Free: With all these awesome features, Realm is completely free.

Cons:

  • Consumes more memory and space as compared to SQLite
  • It’s not a native framework
  • There are not many features to handle relationship between two different tables. If you are using Core Data from a long time, you can phase this situation out. But, users of SQLite will not complain about this situation.

Reference for Realm:
https://realm.io/docs/tutorials/realmtasks/
Above is the link for including Realm in your apps. Link has a video and step-by-step text instructions for integrating Realm in iOS apps from scratch.

Files and Other Options

Below are some of the features available for iOS provided by Apple to store and retrieve data.

NSUserDefaults

It is a class that provides simple storage of different data types solution. It is an interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app. It is perfect for those small bits of information you will need to persist between app launches or device restarts. Data is not secure here, for secure data use keychain access.

Pros:

  • Provided by Apple Inc.
  • Relatively easy to store and retrieve data
  • Perfect for storing small size data (ex: User’s Settings)
  • Simple to use, easy to learn and implement

Cons:

  • It does not have any type of support to search data by using any query
  • Not ideal to store sensitive or large amount of data
  • Performance will decrease, if large amount of data is stored

Plist (Property List)

It is a flexible and convenient format for storing application data. It was originally defined by Apple for use in iPhone devices and later spread to other applications. Since plists are XML files, you can use a simple text editor to translate them.

Although, property lists can also store binary data, they are inefficient with large blocks of it.

Property lists work best with data that takes only a few hundreds of kilobytes. Moreover, there is no mechanism to migrate data as your app evolves and their format changes.

Pros:

  • Simple to understand and easy to use
  • Can be used for project settings

Cons:

  • You can't run complex queries on them. Have to read the entire file into memory to get any data out of it and save the entire file to modify anything on it

NSCoding Protocol

A protocol that enables an object to be encoded and decoded for archiving and distribution.

Use the below mentioned two methods to store and fetch data:

-(void)encodeWithCoder:(NSCoder*) encoder
-(id)initWithCoder:(NSCoder*) decoder

Pros:

  • It is provided by Apple Inc.
  • You can save any view controller with its object without losing any value

Cons:

  • The process is slow
  • No query method is provided. Hence, data to be fetched manually
  • Store data size is big as compared to NSUserDefault and CoreData

If there is an urgent requirement to provide offline support to the application, then one can go with this approach. The entire view controller's data can be stored using NSCoder and fetched again for display. However, even if it is not scalable, it is advised to ship to better alternatives.

Conclusion

Finally, depending upon the needs of your requirements, choose the database that suits your application.

Option/Name

Performance on 10k records

Ease of use for developers

Best suited

Flexible structure (migration)

Pricing

SQLite

Slow when compared with other

Simple to integrate with app. Simple query just needs to run and fetch data.

iOS 2.2 or later (equivalent SQLite version needs to be used)

Possible but complex

Free

Core Data

Faster than SQLite

Easy to use. Less code required to integrate and use it.

iOS 3.0 or later

Possible

Free

Firebase

Depends on network connection

Minimal setup, clear reference is provided by firebase for integration.

iOS 8 or later

NA*

Free for limited connections only

Realm

Faster than Core Data & SQLite

Good documentation & support for integration

iOS 8 or later

Possible

Free

NA*- Not Applicable

  • If you have to choose between Core Data and SQLite, then what should you use, Core Data or SQLite? In short, if you need a lightweight solution and don’t need Core Data’s feature set, then SQLite may fit your needs.
  • On the other hand, if you are managing a complex object graph with many entities, attributes, and relationships, Core Data is definitely worth considering.
  • Firebase database or real-time databases works fine. However, they are not completely free and there are certain limitations that you have to consider. Furthermore, you have to avoid deep (nested) structures and balance duplicate data with consistency. Finally, even though you can implement a lot of your logic in every client (e.g. Android, iOS), sometimes it is better to implement it as a cloud function to avoid code repetition, or to move an intense procedure out of a mobile application. If you want to use for both android and iOS platforms, then you may choose this.
  • Choose Realm if you are trying to store data that uses online/offline sync with a defined structure and if you want good performance over large dataset.
  • Use NSUserDefaults to save only small amounts of data like user configurations and preferences.

About Innominds

Innominds is a leading Digital Transformation and Product Engineering company headquartered in San Jose, CA. It offers co-creation services to enterprises for building solutions utilizing digital technologies focused on Devices, Apps, and Analytics. Innominds builds better outcomes securely for its clients through reliable advanced technologies like IoT, Blockchain, Big Data, Artificial Intelligence, DevOps and Enterprise Mobility among others. From idea to commercialization, we strive to build convergent solutions that help our clients grow their business and realize their market vision.

Interested! For any demos or project discussions, please write to us at marketing@innominds.com and know more about our offerings.

Topics: Mobility

Deepthi Muramshetty

Deepthi Muramshetty

Engineer - Software Engineering

Subscribe to Email Updates

Authors

Show More

Recent Posts