I am using RealmSwift
for my project. However, I'm not sure how to tackle the following:
RMLException
: Attempting to modify object outside of a write transaction - callbeginWriteTransaction
on anRLMRealm
instance first
thrown.
Anyone any clue?
import RealmSwift
func createOrUpdateMachineInRealm(machine: Machine){
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// do some task
let realm = Realm()
realm.beginWrite()
realm.write{
realm.add(machine, update: true)
}
realm.commitWrite()
dispatch_async(dispatch_get_main_queue()) {
// update some UI
actionDelegate?.operationCompleted(true)
}
}
}
Solution: I pass in the parameters for machine as well and assign them to the machine within the realm.write()
func createOrUpdateMachineInRealm(machine: Machine, name: String){
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// do some task
let realm = Realm()
realm.write{
machine.name = name
realm.add(machine, update: true)
}
}
}
*** Terminat ing app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
when i call the method again ( for update). – Margravine