Home » Using Realm database in iOS app

Using Realm database in iOS app

by Online Tutorials Library

Using Realm database in iOS app

We have used SQLite and CoreData to persist the data in iOS applications. The realm is an alternative to Apple’s CoreData. It is faster and easier to use than CoreData and SQLite. It is free of charge for unlimited use until we take advantage of Realm’s Cloud features. In this tutorial, we will create a new iOS project to use Realm to store the data. We will discuss the basic CRUD (create, read, update, delete) operations with the database.

What is Realm

Realm is a database that is built to target mobile applications for iOS and Android. It works better and faster than SQLite and CoreData for iOS applications. It’s easier to use and requires less code to store and retrieve data. It is free to use for iOS and Android apps without any limits. Realm is an alternative for the mobile databases as currently, we don’t have many options other than SQLite or any wrapper that works on the top of SQLite, such as CoreData. It is mainly designed to be easy to use as it not an ORM as it uses its persistence engine to provide speed and performance.

Why use Realm

Realm is superior to other mobile databases in terms of speed. It is easier to use and also requires less code to retrieve and store the data. In this section of the tutorial, we will discuss the advantages and disadvantages of the Realm database.

  1. Easy to install: in iOS, it is very simple to install Realm in applications. We can easily install Realm in iOS applications just by using the Cocoapods.
  2. Works faster than other databases: It is faster to save, retrieve, or performing any database operations in Realm. Realm works faster than SQLite and CoreData for iOS applications.
  3. Cross-Platform: Realm database is cross-platform and therefore can easily work on iOS and android regardless of the technology we are working upon.
  4. Scalability: It is very important to consider the database’s scalability while developing applications that deal with a large number of users and their records. We can use Realm to deal with a huge amount of data in a lesser time. We will also get speed and smoothness while using Realm in the application.
  5. Good documentation & Support: Realm team provides rich documentation about installing and using the Realm database in our applications.
  6. Free: Realm is free with all the benefits it has provided.

Concepts Overview

To understand the proper functioning of the Realm database, we need to know about the classes that we are going to use in this tutorial.

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

Getting Started

First, we need to create a new XCode project. Create a single-view iOS application with swift, as shown below.

Using Realm database in iOS app

In this tutorial, to add the Realm database to our project, we will use Cocoapods. For this purpose, open the terminal, navigate to the project directory, and run the following command.

It will generate the Podfile in the project directory; we need to open it and add the pod to download Realm.

The Podfile will contain the following code.

Now, run the following command in the terminal to install the pod.

Once we are done installing the pods for Realm, we need to close all XCode instances and open the .xcworkspace file.

Using Realm Database for data persistence

First, we need to create an object to use in Realm. For this purpose, add a swift file Employee.swift with the following code.

Now, we need to save and retrieve employees from Realm. Let’s create a class as EmployeeDAO and write the following code into it.

In EmployeeDAO, we have created the Realm database as a class member. Now, we need to add methods to save and retrieve the objects of type Employee.

Here, Realm.write starts a write transaction. A write transaction can fail, so we need to mark it as throwing. Now we need to look at the code to retrieve the objects.

Let’s add a method to add a new employee in Employees Database. We will place this method inside EmployeeDAO class. Add the following Code to EmployeeDAO class.

Now, we will try to save and retrieve the employees in the ViewController class. For this purpose, add the following code in ViewController.

Output

It prints the retrieved employee in the XCode console.

Results <0x7f9aca60e5b0> (  [0] Employee {  age = 24;  id = 1001;  name = john;  }  )  

Now, let’s try to delete the object from the database. For this purpose, add the following method in EmployeeDAO class.

To test the delete method implementation, let’s add a new employee with the name Mike into the Database, delete that, and then retrieve the employee with the name Mike. For this purpose, let’s add the following code in the saveAndGetEmployees method of ViewController.

This will print the following output in Console.

Results <0x7fc1c1f6d040> (  [0] Employee {  age = 30;  id = 1002;  name = Mike;  }  )  Results <0x7fc1c1c11bb0> (      )  

Now, our ViewController will contain the following code.

Our EmployeeDAO class will contain the following code.


You may also like