我为我的项目使用RealmSwift
。但是,我不知道如何处理以下内容:RealmSwift RLMException
RMLException
:试图修改写入事务以外的对象 - 上RLMRealm
例如第一
抛出调用beginWriteTransaction
。
任何人有任何线索?
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)
}
}
}
感谢您的回复@NateMann。我可以在第一次没有错误时添加它。然而,由于未捕获的异常'RLMException',我得到了这个***终止应用程序,理由是:'尝试修改写事务之外的对象 - 首先调用RLMRealm实例上的beginWriteTransaction。'当我再次调用该方法时更新)。 – perwyl
您正在添加一台机器或正在更新一台机器。如果你正在更新一个,比如说machine.name =“Drill”,这个必须在Realm()。write {}中完成,就像这个Realm()写入{machine.name =“Drill”} –
是否有可能我在UI级别更新Machine对象并传递给Realm()(Storage.swift)进行写入事务?我试图避免在ViewControllers中使用Realm。 realmSwift非常新 – perwyl