我正在为我的项目使用Realm
Swift.但是,我不确定如何解决以下问题:
@H_404_22@
RMLException
: Attempting to modify object outside of a write transaction – callbeginWriteTransaction
on anRLMRealm
instance first
抛出.
有人知道吗?
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) } } }
解决方案:我也传入了机器的参数并将它们分配给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) } } }