Realm library in iOS with Swift 5 – PART II

Realm library in iOS with Swift 5 – PART II
July 8, 2020 No Comments Development,iOS Development Pushpendra Kumar

Hey, Guys welcome to this tutorial. In this complete video. I will make you understand the quick trick for migration, deletion, and updating the data in the Realm Library. So basically here you are going to learn 3 basic and simple things about the realm library. Which are names as Migration, Deletion, and Updation. And this is a simple way to achieve our goals. Through this simple video tutorial, you can learn more interesting things about the realm library. So watch the complete video. And if you watched the complete video then do not forget to give your suggestion into the comment box. Because your feedback & suggestions do matter for me 😊😊😊😊👇🏻

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

Great now, Get the entire useful code of the video.

Realm with swift 5 – Migration

let config = Realm.Configuration(schemaVersion : 1, migrationBlock: {migration, oldSchema in if oldSchema < 1{}})
        
Realm.Configuration.defaultConfiguration = config

Great work after that find the code for update the data in this video.

Update

Here, With the help of below code you can understand the functionality of the update data inside the Realm library.

    @IBAction func btnUpdate(_ sender: Any) {
        DispatchQueue(label: "backgraoung").async {
            autoreleasepool {
                let realm = try! Realm()
                let mainModel = realm.objects(MainModelClass.self).filter("age='25'").last
                if mainModel != nil {
                    try! realm.write {
                        let someObject = mainModel?.some_object ?? SomeObject()
                        someObject.title = "Hey This Pushpendra Title..."
                        mainModel?.some_object = someObject
                        
                        let jsonData = try! JSONEncoder().encode(mainModel!)
                        let jsonString = String(data: jsonData, encoding: .utf8)
                        
                        DispatchQueue.main.async {
                            UIView.animate(withDuration: 0.5, animations: {
                                self.view.layoutIfNeeded()
                                self.lbAfterUpdate.text = jsonString
                            })
                        }
                    }
                }
            }
        }
    }

Create job, After that get the code for Delete functionality.

Delete

Here, With the help of below code you can understand the functionality of the delete data from the Realm library.

    @IBAction func btnDelete(_ sender: Any) {
        DispatchQueue(label: "background").async {
            autoreleasepool {
                let realm = try! Realm()
                let model = realm.objects(MainModelClass.self).filter("id='1'").last
                if model != nil {
                    try! realm.write {
                        
                        realm.delete(model!)
                        
                        DispatchQueue.main.async {
                            UIView.animate(withDuration: 0.5, animations: {
                                self.view.layoutIfNeeded()
                                self.lbAfterDelete.text = "Data is deleted"
                            })
                        }
                    }
                }
            }
        }
    }

 

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 *