Realm was implemented by referring to the following article. https://qiita.com/pe-ta/items/616e0dbd364179ca284b You can check the movement from the link below. https://twitter.com/vex3ex5frd2xcvg/status/1350637210106699777?s=21 I've researched various things from there, so I'll write what I understand. Please check the following articles for the implementation procedure.
First, import Realm Swift
.
Try importing each where you need RealmSwift
.
At first, an error will occur, so you can import it by building it once.
ViewController.swift
import UIKit
//Importing RealmSwift gives an error, but building it cures
import RealmSwift
Create a swift file by adding a file.
This file defines the data items handled by Realm and their types.
In short, it is an image of saving values in this property.
Realm model properties must have @ objc
and dynamic
.
TodoModel.swift
import Foundation
import RealmSwift
class TodoModel: Object {
@objc dynamic var koumoku: String? = nil
}
It seems that Results
is" the automatic update container type of Realm returned from the object ".
You will be adding data to this variable.
ViewController.swift
//Prepare the created TodoModel type variable.
//Variable to hold the data received from Realm
var itemList: Results<TodoModel>!
Instantiate the Realm used this time so that it can be used.
Then use object ()
. Since this is the one that can get all the data specified in ()
Get it and put the obtained data in the variable created in ③.
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
//Instantiate Realm
let realm = try! Realm()
// objects()Gets all the specified data stored in Realm!
// object().filter()It seems that you can further filter and specify with.
self.itemList = realm.objects(TodoModel.self)
}
This time, if you press the button, the content written in the textField will be added. Please write in the Action you want to add / save.
textField.text
in the koumoku
property created in ②.realm.write
was in the official docs to update the Realm instance to the latest version and create a notification if applicable.
Since the data is updated in 2, the latest one is acquired by realm.write
.
realm.add ()
adds unmanaged objects to Realm in parentheses.
In other words, add unmanaged (added data) to Realm with realm.add ()
and
You get the latest version with realm.write
.ViewController.swift
@IBAction func addButton(_ sender: Any) {
// 1.Instantiate a model class. You can now access it!
let instanceTodoModel: TodoModel = TodoModel()
// 2.Insert the value of textField
instanceTodoModel.koumoku = self.textField.text
// 3.Instantiate Realm and get database! The one I went to with viewDidLoad
let realm = try! Realm()
// 4.Add the value of textField to the database
//write is refresh()Updates the Realm instance to the latest Realm version as if was called and generates a notification if applicable.
try! realm.write {
realm.add(instanceTodoModel)
}
self.tableView.reloadData()
}
Since the itemList
contains the data received from Realm, we will use it to create a TableViewCell.
ViewController.swift
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.itemList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
let item: TodoModel = self.itemList[indexPath.row]
cell.textLabel?.text = item.koumoku
return cell
}
Viewcontroller.swift
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
//Among them, instantiated so that Realm can be used
let realm = try! Realm()
if editingStyle == .delete {
//Search for the data you want to delete. This time, the index path of the cell you want to delete.I want to delete the row th item, so specify it with object.
let deleteItem = realm.objects(TodoModel.self)[indexPath.row]
do{
//write is refresh()Updates the Realm instance to the latest Realm version as if was called and generates a notification if applicable.
try realm.write{
//Deleted version of add at the time of addition
realm.delete(deleteItem)
}
}catch{
print("Error")
}
}
tableView.reloadData()
}
ViewController.swift
import UIKit
//Importing RealmSwift gives an error, but building it cures
import RealmSwift
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var tableView: UITableView!
//Prepare the created TodoModel type variable.
//Variable to hold the data received from Realm
var itemList: Results<TodoModel>!
override func viewDidLoad() {
super.viewDidLoad()
//Instantiate Realm
let realm = try! Realm()
// objects()Gets all the specified data stored in Realm!
self.itemList = realm.objects(TodoModel.self)
}
@IBAction func addButton(_ sender: Any) {
//Instantiate a model class. You can now access it!
let instanceTodoModel: TodoModel = TodoModel()
//Insert the value of textField
instanceTodoModel.koumoku = self.textField.text
//Instantiate Realm and get database! The one I went to with viewDidLoad
let realm = try! Realm()
//Hit add from a Realm instance to add a record to the database
//Add the value of textField to the database
//write is refresh()Updates the Realm instance to the latest Realm version as if was called and generates a notification if applicable.
try! realm.write {
realm.add(instanceTodoModel)
}
self.tableView.reloadData()
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.itemList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
let item: TodoModel = self.itemList[indexPath.row]
cell.textLabel?.text = item.koumoku
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
let realm = try! Realm()
if editingStyle == .delete {
//Search for the data you want to delete. This time, I want to delete the indexpath item of the cell I want to delete, so specify it with object.
let deleteItem = realm.objects(TodoModel.self)[indexPath.row]
do{
//write is refresh()Updates the Realm instance to the latest Realm version as if was called and generates a notification if applicable.
try realm.write{
realm.delete(deleteItem)
}
}catch{
print("Error")
}
}
tableView.reloadData()
}
}
TodoModel.swift
import Foundation
import RealmSwift
class TodoModel: Object {
@objc dynamic var koumoku: String? = nil
}
I've tweeted it, so I'll post a link. https://twitter.com/vex3ex5frd2xcvg/status/1350637210106699777?s=21
https://qiita.com/pe-ta/items/616e0dbd364179ca284b https://naoya-ono.com/swift/realm-update-delete/
Recommended Posts