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.
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.
First, we need to create a new XCode project. Create a single-view iOS application with swift, as shown below.
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.
It prints the retrieved employee in the XCode console.
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.
Now, our ViewController will contain the following code.
Our EmployeeDAO class will contain the following code.