Updated 20 January 2021
Hi guys, today we will learn about how to implement CRUD Operation with realm() in an iOS application.
Before starting to code first we need to know what a Realm() is
Realm() is a cross-platform mobile object database. It’s very fast, performance, and easy to use as compared to core data and SQLite. It is written in cross-platform C++, so it works exactly the same way on Android, iOS, macOS, or any other platform.
Most of the Realm() is open-source, but the secret sauce behind Realm() Platform is the core DB engine written from scratch in C++.
Create – In it, we can create a realm() Data.
For example-
1 2 3 4 |
let realm = try! Realm() try! realm.write { // your code } |
Read – In it, we can read a realm() Data.
For example-
1 2 |
let realm = try! Realm() guard let info = realm.objects(Info.self).first else {return} |
Update – In it, we can Update a realm() Data.
For example-
1 2 3 |
try! realm.write { realm.add(name of change field, update: .modified) } |
Delete – In it, we can Delete a realm() Data.
For example-
1 2 3 4 |
try! realm.write { realm.deleteAll() } |
we have taken an example where we know how to create, save, and update data in the realm().
step 1:-
create realmModel.swift file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import Foundation import UIKit import RealmSwift class PersonDetail: Object{ @objc dynamic var name = "" @objc dynamic var age = "" @objc dynamic var dateOfBirth = "" convenience init(name: String, age: String, dateOfBirth:String) { self.init() self.name = name self.age = age self.dateOfBirth = dateOfBirth } } |
step 2:
Create a view controller
step 3:
Create Table view Cell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import UIKit class RealmTVTableViewCell: UITableViewCell { @IBOutlet weak var nameLabel: UILabel! @IBOutlet weak var dateOfBirthLabel: UILabel! @IBOutlet weak var ageLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animates: Bool){ super.setSelected(selected, animated: animated) // Configuration the view for the selected state } } |
step 4
Create view Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import UIKit import RealmSwift class ViewController: UIViewController { @IBOutlet weak var tableViewOutlet: UITableView! var buttonAlertBar = ButtonAlert() let items = try! Realm().objects(PersonDetail.self) let realm = try! Realm() override func viewDidLoad() { // create realm data if realm.isEmpty { try! realm.write { realm.deleteAll() } } } @IBAction func clickBarButton(_ sender: Any) { buttonAlertBar.alertAction(controller: self, value: 0, nameString: "" ) buttonAlertBar.onEdit = { [weak self] (tableReload: Bool) in self!.tableViewOutlet.reloadData() } } } extension ViewController: UITableViewDataSource,UITableViewDelegate{ func tableView(_tableView: UITableView, numberofRowInSection section: Int)-> { return items.count } func tableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let myCell = tableViewOutlet.dequeueReusableCell(withIdentifier: "RealmTVTableViewCell") as! RealmTVTableViewCell myCell.nameLabel.text = "Name :" + items[indexPath.row].name myCell.ageLabel.text = "Age :" + items[indexPath.row].age myCell.dateOfBirthLabel.text = "DOB :" + items[indexPath.row].dateOfBirth return myCell } func tableView(_tableView: UITableView, heightForRowAt indexPath: IndexPath) -? CGFloat { return 120 } func tableView(_tableView: UITableView, commit edityingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { // delete data in realm if editingStyle == .delete { try! realm.write { realm.delete(items[indexPath.row]) } self.tableViewOutlet.deleteRows(at: [indexPath], with: .left) } } } |
It looks like this
. .
Conclusion
In this blog, we have discussed how to save, update, and delete the data in the database by using the CRUD Operation with Realm().
I hope this blog will help you in getting some basic knowledge about the CRUD Operation with Realm().
Thanks for reading!
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.