One of the most important components of modern applications is data storage. In an ever connected world, data is involved in almost everything. One of the things a developer has to consider when creating an application is how to store this data in a way that leads to the best experience for the user. In this article, we are going to look at databases for iOS including hosting options and database types, and look to answer the question, “Which is the best iOS database?”

What is a database for iOS?

A database for iOS is a database intended specifically for use in persisting data in iOS applications. It often stores data locally on the device to enable access to the data even when offline, due to the ever changing network connectivity of an iOS device.

The most common database on iOS is SQLite, as this is included in the iOS SDKd, thus available to developers out of the box. We will discuss SQLite and other database options later in the article.

Choosing the best database for iOS

Although databases all provide the ability to store, query, and manipulate data, not all databases are created equal.

Before deciding on the best database for your iOS application, you need to know the requirements of your application. Here are two questions that can help you identify these:

  1. What sort of data will you be storing?
  2. Are you going to store structured or unstructured data or even large files? Should the data be available across multiple users and/or devices, or can it be stored only on the device?

  3. How will the data be accessed?
  4. Will the data be stored across multiple data sources, requiring complex queries to join the data?

Different types of iOS database

There are multiple different database providers that are compatible with iOS but they broadly fall under three categories: relational database, key-value database, and object-oriented datrabase.

Database Type

Characteristics

Relational database.

Data is stored in tables. The tabular format uses keys to form relationships between the tables. The most common example for iOS databases is SQLite using Core Data as a persistence framework. This is the choice if the relationship between the data is heavily connected

Key-Value database (or Key-Value store)

This uses a key (sometimes known as field) to value mapping, to store the information. It’s most commonly used in iOS to store small bits of information available at app launch. An example would be UserDefaults, available out of the box with the iOS SDK.

Object-oriented database.

This can work with complex data objects as used in object-oriented programming languages, such as Swift. It’s able to do fast queries with complex data. Code is often simpler as well due to closeness in structure between the database and object in code. Realm is an object-oriented database.

Types of mobile data storage hosting

Where a database is located, aka hosted, is an important factor with iOS databases. The following factors, therefore, are worth considering:

      1. Local storage: Having the data available offline by storing it locally is an important feature in mobile applications. Internet connection can be inconsistent on mobile devices. With data stored locally, apps continue to be functional, even when offline, leading to a much better user experience.

      2. Cloud: Data sharing across devices and a centralized back end is a common feature of iOS applications. However, this functionality is not supported by local-only storage solutions. To overcome this, mobile developers will need to save the data from their apps to the cloud or a self-hosted server. A cloud database has several advantages over traditional storage.

      For example, MongoDB Atlas, MongoDB’s multi-cloud application data platform, MongoDB’s multi-cloud application data platform, combines a flexible document model and unified query interface with automated horizontal and vertical scaling, flexibility, easier database sharing between project types (including mobile), and much more, to provide a first-class developer experience. However, the driver used to communicate with Atlas requires network access at all times, meaning a less optimal user experience when offline or with a slow connection.

      3. Server: You may prefer to host your data using your own servers or data center. For this reason, you will want to consider a database that supports this type of storage location and connection when persisting data for mobile. However, this still requires internet access so raises the same concerns around connectivity.

      4. Cloud and local storage: One way to solve the issues of connectivity is to store data both locally and in the cloud using Device Sync . This leads to fast, real-time, reactive mobile apps that save data in the cloud and locally. It takes care of the complexities of syncing data between two data sources with networking code, conflict resolution, and error handling, which improves developer productivity.

Advantages of object-oriented databases for iOS

There are a few advantages to consider that make object-oriented the best database type for iOS apps:

      1. Simplicity: Swift, like Objective-C, is an object-oriented programming language, which means that the data being persisted to a database will start out as objects in the application code. If the data was being persisted into a relational database, an object relational mapper (ORM) would need to be used to convert these objects into the tabular, row-based storage of relational databases. This ORM takes additional time and code to implement, slowing down development time.

      Object-oriented databases were created to keep the data consistent between how it’s used in code and where it’s stored. This means it’s a great solution for iOS apps written with Swift, because there won’t be a need to map the objects for data persistence. Complex data can be stored as objects, avoiding any potential unintended consequences due to the conversion to a row in a relational database table.

      2. Performance: Sometimes, there will be complex object-to-object relationships between the data. By using an object-oriented database, queries across those objects will be much faster.

      3. Readability: The data is much easier to read, both in the database and when used in the application, because it shares the same shape.

Use cases for iOS object-oriented databases

There are, in fact, many use cases for using an object-oriented database for iOS. Due to its general purpose object-oriented language, Swift is a natural fit for object-oriented databases and can be used whenever there are objects in code that need to be persisted.

How does Realm compare?

Let’s examine some of the most popular examples of databases and then look into Realm. This is not an exhaustive list:

Product

About

Pros

Cons

SQLite

This is the most well known relational database across both iOS and Android.

It’s an open-source, relational database compatible with Swift.
It can be used across multiple platforms, such as iOS and Android, because it uses a file-based system.

Objects need to be transformed into rows to be stored.
It doesn’t provide any cloud hosted, cross-device, or sync capabilities.
Only the application has managed security. There’s no user-based security.
It may suffer from performance issues due to not being built for large applications.
SQLite lacks scalability due to the restrictions of relational databases.
Oftentimes, it requires a DAO or ORM (like Core Data) to be used as well.

Core Data

Core Data is actually an object graph management framework rather than a database itself.
It’s therefore paired with a database, most often SQLite. It can, however, also be used to store data in XML, in-memory, and binary formats.

It’s good at managing complex objects.
It offers powerful querying capabilities.
It also offers additional features, such as input validation.
It supports lazy loading of data, improving performance
The graph part of the framework allows for an easy visualization of the entities.

Because it’s just a framework, it inherits the disadvantages of the database it uses.
It has a steep learning curve.
It comes with additional overhead on top of just using a database.

Firebase - Cloud Firestore or Realtime Database

Firebase is a mix of features that can be mixed and matched to fit app requirements. One of these is a realtime database.
It’s a cloud-based key-value store with sync capabilities.

It’s accessible from an SDK, making it easy to get started and build an MVP.
As it’s cloud-hosted, it has the advantages of scalability.
It has a strong community for support.
There is a wide array of products available beyond just a mobile database, such as authentication.

There’s an increased app size due to adding a third-party library.
Firebase has lower performance and poor user experience due to limited data model and query constraints.
Complex queries have to be offloaded for handling by Cloud Functions, creating additional management of the service burdens on developers.
There are no aggregation queries (average, sum, count, etc).
Sharding is required for scaling but cannot be manually configured in Firestore.
Sync only supports “last write wins” for resolving conflicts, which can lead to data loss in applications that support multiple users.

UserDefaults

This is an interface for access to a key-value database for storing user default values across app launches.

It can store values such as default language or media playback speed, keeping it consistent per user for as long as the app is installed.
It uses an in-memory cache which leads to faster performance.
Data is included in backup and restore within iOS.

UserDefaults has limited use case.
It offers limited data type support.
If too much is being stored, stretching the device’s free storage space available, the in-memory cache can slow the app down.
It’s not intended for large amounts of data.
Data stored is not encrypted, so it cannot be used for sensitive data.

KeyChain

It’s an API used for persisting sensitive data locally, such as passwords and in-app purchase statuses.

Data is encrypted, so it’s ideal for storing sensitive information.
It’s built into iOS through the security framework, meaning it doesn’t require extra libraries.

The framework is quite old and hasn’t been updated recently.
APIs haven’t been modernized, so when used with Swift, the code can be quite complex.

Realm

Realm is a fast, reactive, and scalable alternative to SQLite that makes storing, syncing, and querying data simple for modern mobile applications.

It works with objects idiomatically thanks to the object-oriented data model, so no need for ORMs or DAOs.
Realm can scale complex use cases with ease.
It supports edge-to-cloud sync with MongoDB Atlas and Device Sync.
It can easily and reliably persist data on-device without hurting developer productivity or slowing down app performance.

The app size increases due to requiring adding a third-party library.
It has a smaller, albeit still active community compared to SQLite and Firebase.
While Atlas is available across multiple cloud providers, Realm’s sync functionality currently only runs on AWS, with support for Azure and GCP coming soon.

Summary

Like many projects targeting other platforms, creating an app for iOS often requires a database. This article has helped inform you on your choices for the best database for iOS. This has included what to consider when choosing an iOS database, the different types of databases ​​available, different hosting options, and why you might consider object-oriented databases such as Realm.

Realm has many SDK's available, including the Realm Swift SDK, so you can get started using it today.


FAQ

What is Swift?

Swift is Apple’s proprietary general-purpose programming language, developed as a successor to Objective-C. It’s used to create projects ranging from mobile and desktop apps to cloud hosted apps.
It adds some improvements over Objective-C such as type inference, type safety, performance improvements, and a much more concise language, requiring fewer lines of code.
Because Swift, like Objective-C, was also developed by Apple for their platforms, it gives access to the Foundation framework, providing access to some useful native features that play a role in data persistence:

  • UserDefaults: This gives the ability to store user- or application-specific default information, such as preferred playback speed or language.
  • Keychain: This is where secure data is stored, such as passwords and certificates, and will be well-known to iOS developers due to its role in provisioning devices and applications, and for development.

We will discuss some other examples of iOS data persistence that work well with Swift when we discuss database options later in the article.

Are Realm and MongoDB the same?

No. In 2019, MongoDB acquired Realm and has gone on to release Device Sync — a mobile-to-cloud sync solution allowing the syncing of data from Realm to Atlas, MongoDB’s fully managed cloud data platform.

Realm, Stitch, MongoDB?

The terms Realm, Stitch, and MongoDB are sometimes used interchangeably, which can lead to confusion and people asking if Realm and MongoDB are the same. The following list will explain what each product or term means and how they connect to attempt to remove any ambiguity:

  • MongoDB: MongoDB is the name of the company that has developed many of the products discussed in this article. MongoDB started out by creating their core database product that could be installed locally or in data centers. It then evolved to have MongoDB Atlas, the database-as-a-service in the cloud. MongoDB Atlas today is an application data platform with a wide variety of features beyond just the database.

    MongoDB acquired Realm and has evolved it to include a device-to-cloud synchronization service that syncs back to MongoDB Atlas.

  • Realm: Realm is an open source, fast, scalable alternative to CoreData and SQLite that makes storing, syncing, and querying data simple for modern iOS applications.

  • Stitch: MongoDB Stitch was a serverless backend-as-a-service that provided an easy way to build modern cross-platform applications on top of MongoDB. This was rebranded and became part of the wider Atlas application data platform.

We will discuss some other examples of iOS data persistence that work well with Swift when we discuss database options later in the article.


Related

Deploy Realm in minutes

Deploy an iOS, Android, or cross-platform “To Do” app with real-time sync in minutes.

Get started now