Realm library in iOS with swift 5 – PART I

Realm library in iOS with swift 5 – PART I
July 4, 2020 No Comments Development,iOS Development Pushpendra Kumar

Hey, Guys welcome to this tutorial. In this complete video, I will make you understand about the quick trick. Ans the trick is how you Realm library in iOS with Swift 5. And this is a simple way to achieve our goals. By this small trick, you can make your iOS application more attractive and more function-able with the help of the Realm library and its functionality. So watch the complete video. And if you watched the complete video then do not forget to give your suggestion. Because your feedback & suggestions do matter for me πŸ˜ŠπŸ˜ŠπŸ˜ŠπŸ˜ŠπŸ‘‡πŸ»

Realm library

And finally, I would like to say that, If you like this video and it’s helpful for your project then click on like button. And do not forget to subscribe to this YouTube channel for future updates.

Great, Below is the important code related to this tutorial. As you know and also mention into the above video you have to install the pod library with from the following pod. And If you do not know how to install pod in an iOS then watch the above video.

pod 'RealmSwift'

After installing find the model Classe as mention into above video.

//
//  Model.swift
//  Realm Example
//
//  Created by Pushpendra on 04/07/20.
//  Copyright Β© 2020 Pushpendra. All rights reserved.
//

import RealmSwift


class MainModelClass: Object, Codable {
    @objc dynamic var id : String?
    @objc dynamic var name : String?
    @objc dynamic var some_object : SomeObject?
}

class SomeObject: Object, Codable {
    @objc dynamic var id : String?
    @objc dynamic var title : String?
    @objc dynamic var some_integer : Int = 0
    @objc dynamic var some_double : Double = 0.0
    var some_array : List<SomeArray> = List<SomeArray>()
}

class SomeArray: Object, Codable {
    @objc dynamic var id : String?
    @objc dynamic var name : String?
    @objc dynamic var value : String?
}

Great work. After that get the code for inserting data into Realm library.

    //
    //  ViewController.swift
    //  Realm Example
    //
    //  Created by Pushpendra on 04/07/20.
    //  Copyright Β© 2020 Pushpendra. All rights reserved.
    //
    //MARK: Function for inserting data into realm library
    @objc private func tapOnMe(){
        let list  = List<SomeArray>()
        var obj = SomeArray()
        obj.id = String(Date().timeIntervalSince1970)
        obj.name = "Array Object 1"
        obj.value = "Array Object value one"
        list.append(obj)
        
        obj = SomeArray()
        obj.id = String(Date().timeIntervalSince1970)
        obj.name = "Array Object 1"
        obj.value = "Array Object value two"
        list.append(obj)
        
        let someObject = SomeObject()
        someObject.id = String(Date().timeIntervalSince1970)
        someObject.title = "Some title"
        someObject.some_integer = 12
        someObject.some_double = 123456.8989
        someObject.some_array = list
        
        
        let mainModel = MainModelClass()
        mainModel.id = String(Date().timeIntervalSince1970)
        mainModel.name = "Pushpendra Kumar Saini"
        mainModel.some_object = someObject
        
        
        realm?.beginWrite()
        realm?.add(mainModel)
        try! realm?.commitWrite()
    }

Great and finally get the code for Read the data from the realm library database

    //
    //  ViewController.swift
    //  Realm Example
    //
    //  Created by Pushpendra on 04/07/20.
    //  Copyright Β© 2020 Pushpendra. All rights reserved.
    //
    //MARK: Function for reading data into realm library
    private func readData(){
        realm?.beginWrite()
        let data = realm?.objects(MainModelClass.self)
        let jsonData = try! JSONEncoder().encode(data!)
        print(String(data : jsonData, encoding: .utf8)!)
        try! realm?.commitWrite()
    }

Great work 😊😊😊😊

In Conclusion, You have done with this tutorial. And hope now the concept of Realm library is clear. If you have any doubts regarding the Realm library in iOS with Swift 5 then you can comment into the comment box. And if you like this tutorial with a complete description of the simple and attractive tutorial in iOS then pleaseΒ like my videoΒ on YouTube and do not forget to subscribe to myΒ YouTube channel. Because you will get lots of videos related to swift development with very sort tricks.

Besides, If you are more curious about the development and knowing about tutorials then follow the below links πŸ‘‡πŸ»πŸ‘‡πŸ»πŸ‘‡πŸ»πŸ˜Ž

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

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