I was able to save and even register saved data from Realm swift, however I was struggling to understand how to be update data using those saved values.
In my code I have a didselectrowat instance where I change the text in a label using a set of tableview values, which when tapped automatically change the label values and save those label values to realm studio. I want it so that instead of just updating when the app is running, if I close the view, my label (which changed when the view was running originally) automatically defaults to the previously selected tableview value (such as in view did load) this is what I have so far:
class ViewController: UIViewController {
@IBOutlet weak var WelcomeTo: UILabel?
@IBOutlet var Language: UILabel?
@IBOutlet var tableView: UITableView!
@IBOutlet weak var NextButtonText: UIButton!
@IBAction func NextButton(_ sender: Any) {
}
let realm = try! Realm()
var categories: Results<Category>!
//MARK: - viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "NewTableViewCell", bundle: nil), forCellReuseIdentifier: "ReusableCell2")
tableView.dataSource = self
tableView.delegate = self
WelcomeTo?.text = Category().welcomeTitle
Language?.text = Category().languageTitle
loadCategories()
let quicksave = realm.objects(Category.self)
print(quicksave)
}
func save(category: Category) {
do {
try realm.write {
realm.add(category)
}
} catch {
print("error at saveCategories() - (error)")
}
}
func loadCategories() {
categories = realm.objects(Category.self)
}
}
…
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
WelcomeTo?.text = cells[indexPath.row].welcomeToTextBar
Language?.text = cells[indexPath.row].languageTextBar
let newCategory = Category()
newCategory.welcomeTitle = (WelcomeTo?.text)!
newCategory.languageTitle = (Language?.text)!
self.save(category: newCategory)
}
}
I tried using the various keywords given in the swift realm package manager and even tried with an experimental value of “quickSave” (which did not work).