Hi I’m trying to change the variable ‘category’ from an object called ‘book’ in realm with the following code:
alert.addAction(UIAlertAction(title: "Reading", style: .default, handler: { (_) in
try! realm.write {
let category = "reading"
let book = Book()
book.category = category
}
}
When I checked in mangoDB realm studio the object category has not been updated. The tutorials I looked at used the same function. This is the whole updated code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
func showmethisfunction() {
let realm = try! Realm()
let boook = Book()
let alert = UIAlertController(title: "Want to put your book in a list?", message: "Please Select an Option", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Read", style: .default, handler: { (_) in
try! realm.write
{
boook.category = "read"
}
}))
alert.addAction(UIAlertAction(title: "Want to read", style: .default, handler: { (_) in
try! realm.write {
boook.category = "wanttoread"
}
}))
alert.addAction(UIAlertAction(title: "Reading", style: .default, handler: { (_) in
try! realm.write {
let category = "reading"
boook.category = category
}
}))
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: { (_) in
print("User click Dismiss button")
}))
self.present(alert, animated: true, completion: {
print("completion block")
})
}
showmethisfunction()
}
I used a similiar alert controller to add a book manually, and that worked, it just doesn’t work when I want to add a variable from a book
3
Answers
First get your object which one you can update.
I’m given example using filter. You can use any.
Or get idea from this link
https://docs.mongodb.com/realm-legacy/docs/swift/latest/index.html#models
Go to Auto-updating objects section in above link.
I updated my answer after updated your question.
Replace line
let boook = Book()
with this linelet boook = realm.objects(Book.self).first ?? Book()
To restate the question:
There are a number of different ways to do that and some of the answer depends on the object model.
Generally speaking, most objects would have some unique identifier called the primary key and if you know that identifier, updating the object is a snap
The ObjectId will generate a unique _id for every object when it’s instantiated. If you know the primary key, you can update an object like this
This assumes the existing object you want to update has a primary key of "1" and will update the object’s name property to Jay 2
You can also read the object in via the primary key to then update it
lastly you can run a query to get an object with a matching property as well (but don’t do this)
This last option is generally a bit ‘dangerous’ as it will filter for all PersonClass objects with the name of ‘Jay’ and the results will be unordered so the
first
one could be different at different times… so the key here is ‘don’t do it this way’.As a side note per the above, realm results are unordered unless you specify the
.sorted(byKeyPath:
.You can update the existing object without creating primary key in RealmSwift.
import Foundation
import RealmSwift
final class ModelRealM: Object {
}
class ChattingVC: UIViewController {
let chattingVM = ChattingVM()
let realm = try! Realm()
var arrData : Results!
}
class ChattingVM{
}