Press "Enter" to skip to content

Getting Started with Realm using Swift 3.0

Greetings Humans!

I was thinking for a long time to write about Realm. So…

What is Realm?

Realm is an open source client side database for Mobile platforms. Read more on their web page.

Its quite easy to use and less complicated as compared to Core Data. In this tutorial I am going to guide you through basic functions of it which are Add, Update and Delete.

Setting Up Project

Create a project and initiate a pod in it, then open the pod file and paste the following snippet in it.

After that save it and install the pod. It might take some time to install.

If you don’t want to use pod to install Realm, you can also download the required files and drag it in your project.

And your project is ready.

Constraining the View Controller

I have made a View Controller and created all the outlets and actions of the views on it.

storyboard

 

Adding Model Class

Additionally, I also have created a Model class which in other words you can say table. With id as the primary key.

 

Adding Object to Realm

Now, open your ViewController class and add two variables to it.

We are going to create a function to check if Realm already contains the primary key we are trying to add in.

I have also created some helper functions.

Finally, In action of your Done button. Every transaction (add, update or delete) in Realm needs to be done inside a write block. So first we initialized the object of Model class, then we passed data to the properties inside it and then added it to realm.

 

Updating Object in Realm

Updating procedure is somewhat similar to adding object, but first we search for the object we want to change and then update it. This code will go inside your Update button action.

 

Deleting Object from Realm

Deleting is pretty straight forward. Check if the object exists in realm, then inside write block delete it from realm.

 

Displaying on TableView

In datasource functions of the tableview.

 

Lastly, Build and run it. I got the following results.

realm_result

The source code can be found here.
If you have any questions please leave a comment. 🙂

Good day!

Author: Aaqib Hussain

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he’s probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

4 Comments

  1. farooq
    farooq June 6, 2017

    Great effort, its amiazing!!!

  2. Faran Mushtaq
    Faran Mushtaq July 11, 2017

    Great !!

  3. QuocLoi
    QuocLoi August 8, 2017

    Great Tutorial!
    I found many tutorial about using Realm but most of them is making simple ToDo app and just use the add object function. You have done good job.
    Beside, if I were you, I would code a little different in some points:
    1. No need to put conditions in brackets ()
    2. I avoid calling self if not necessary (only in closure and initialiser if needed)
    3. Function returns a boolean should be named started with “is”, let say, in my code
    func isTextFieldEmpty() -> Bool {
    if nameTextField.text == “” || idTextField.text == “” {
    // And return TRUE if they are really empty (not false like yours)
    return true
    }
    return false
    }
    it’s like normal English and we can call these condition by: if !isTextFieldEmpty {}
    4. In updateButtonTapped action, why we have to initialise an model object, then set its property according to object, then assign the object = model, then update the object to realm instead of doing like this:
    try! realm.write {
    object!.name = nameTextField.text
    realm.add(object!, update: true)

    Thanks

    • Aaqib Hussain
      Aaqib Hussain August 8, 2017

      Hi QuocLoi. I will keep these things in mind from next time. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *