Database

Getting Started with Realm Database for iOS

Introduction
Getting Started with Realm Database for iOS

In this tutorial we will study the fundamentals of Realm Database, from putting in it to constructing the fashions and writing the create, read, replace and delete methods.

If you are searching for an in depth Realm Database tutorial, then this is now not the article for you. I am searching to write some extra focussed articles on Realm in the future. Once I do, I will hyperlink to them here.

Step 1: Adding Realm Database to your project
There are a few approaches to putting in Realm, however in this tutorial we will be the usage of CocoaPods.

If you do now not have CocoaPods set up you can deploy it by using going to the root listing of your undertaking and strolling the following command, sudo gem set up cocoapods . I tried the usage of the present day CocoaPods, which is 1.9.0 at the time of writing, however it did no longer work for me. If you run into the equal problem you can use CocoaPods 1.6.0. To set up 1.6.0 you can use the following command, sudo gem deploy cocoapods -v 1.6.0

After doing that you want to run pod repo update. This command will replace CocoaPods so that is is aware of about the today's model of Realm.

Next you want to create a Podfile. To do this create a new file known as Podfile in the root listing of your project. Add the following to your Podfile:

After adding the above to my Podfile, it looks like this:

I up to date my platform to iOS 11.

Once you have created your Podfile and introduced the code from above you want to run pod installation in your root directory. By doing this CocoaPods will set up Realm into your project.

Once it is established you want to shut your undertaking if you have opened it with Xcode. CocoaPods requires us to use the xcworkspace file when opening the mission and now not the xcodeproj file.

Step 2: Import RealmSwift
I am going to be the usage of the default ViewController file for this complete tutorial. If this was once a actual mission I would break up it up into exceptional parts.

To import Realm into the ViewController, all we want to do is add the following import:

Step 3: Create the Models
When we are growing a mannequin we want that mannequin to be a subclass of Object. In this tutorial we will be developing a furnishings store, so our two fashions will be Furniture and Store.

To create these two models, add the following to the ViewController file:

The Furniture type has one property, name, and our Store category has two properties, the keep title and a listing of its furniture. Both fashions additionally have a create technique which will permit us to create a new instance.

Step 4: Write to Realm
Now that we have our fashions setup, we can begin saving records to our database. To write, we are going to create a easy technique that will write facts to our database. This write approach will now not take any parameters as it is for tutorial purposes.

Add the following approach to your ViewController:

The above write feature creates two Furniture instances, one being desk and one being chair, we additionally created one occasion of Store. When developing the desk and chair we simply supply it with a name, for the save we supply it with a title and we bypass via the desk and chair that we simply created.

When we create these cases they are solely in reminiscence and have now not been written to our database. To write it to our database we want to wrap the add technique with try! realm.write as proven in the above code.

In Step eight we will name all the techniques that we are going to create. If we name it now it will keep the above statistics each and every time the app runs.

Step 5: Read from Realm
We have our write approach created. Which will permit us to write information to our database, now we want to study the statistics from the database. Once again, as with all these methods, they are simply created for tutorial purposes. If one used to be going to write this for manufacturing we would prefer to be in a position to select what to read, write, replace and delete from the database.

But for now, let's create the examine method. Add the following code under the write method:

All the above approach will do is fetch the objects that we inform it to fetch. In the above code we ignore thru Store.self which will inform realm to fetch all the shops that we have saved.

At the second we have no longer saved anything. But we will be saving stuff later on. For now, we can go on to updating data.

Step 6: Update present data
In this replace technique we are going to study the first object of fixtures in the database and we will replace the title of it.

In our case, the first object of fixtures will be the desk due to the fact that is what we saved first in the write method. We have now not known as the write approach yet, however we will name it in Step 8.

For now, we can add the replace technique under the examine method:

The above code is a mixture of our examine and write methods.

In the code above we strive and get the first Furniture item. It it exists, we will write it to realm with the aid of updating the property price interior try! realm.write.

After the cost is saved, we will print out the first object of furniture. This will have the up to date name.


Step 7: Delete data
The delete technique is very comparable to the replace method.

Add the following code beneath the replace method:

As with the replace method, we will get the first fixtures object that we have saved. If it exists, we will delete it. Realm has a delete approach constructed in. To use the delete technique we want to wrap it in try! realm.write as we would commonly do when writing to the database or when updating the database.

After we have deleted the furnishings item, we print out all the fixtures objects that are in the database and we print out the store. The save will now solely have one furnishings item.

Step 8: Calling the methods
Now that all of our techniques have been created, we can name them. I have referred to as all these techniques from the viewDidLoad method. Update your viewDidLoad approach to seem like the following:

Conclusion
And that is it! Realm is extraordinarily effortless to begin with however the greater query is have to you use it? Would it be higher than the use of Core Data? In most instances I in my opinion select the equipment that are created by way of Apple, however if Realm fits your use case best, then it would make feel to use it.

Download Source Code:

Download

Share post on Social Media

0 Comments

Leave Replay