问题描述
我正在为项目使用RealmSwift
.但是,我不确定如何解决以下问题:
投掷.
有人知道吗?
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)
}
}
}
我只是遇到了与您相同的问题和例外.尽管 Nate Mann 的答案不是解决问题的方法,但它使我朝着正确的方向前进.
您无法修改先前从数据库中拉出的Realm对象,因为当Realm对象(无论出于何种原因)位于其他队列中时,它将尝试对其进行更新并引发错误.
因此,您必须在realm.write{ }
语句中进行所有修改,或者使用相同的主键创建一个新对象,以便正确地对其进行更新.这也意味着您无法使用一个功能来创建 或 更新,而需要两个单独的功能.
您的更新功能必须看起来像这样:
func updateMachineInRealm(machine: Machine){
var updatedMachine = Machine()
updatedMachine.name = machine.name
updatedMachine.value = machine.value + 42
updatedMachine.primaryKey = machine.primaryKey
// "transfer" or modify all the values of the old machine object
let realm = try! Realm()
do {
try realm.write() {
realm.add(updatedMachine, update: true)
}
}
请记住,此代码需要唯一的主键才能起作用,因为这是Realm将与数据库中的新对象匹配的键.
此外,当在一个更大的项目中使用多个线程访问Realm对象等时,这当然也有缺点.但是它会在较小的项目中起作用(例如我正在从事的工作,而您似乎正在从事)./p>
I am using RealmSwift
for my project. However, I'm not sure how to tackle the following:
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)
}
}
}
I just had the same problem and exception as you did. Though Nate Mann's answer is not the solution to the problem, it lead me in the right direction.
You can not modify a Realm object you have previously pulled out of the database, because Realm will try to update it and throw an error, when it is (for whatever reason) on a different queue.
So you either have to do all modification inside your realm.write{ }
statement or create a new object with the same primary key, so it gets updated correctly. That would also mean that you can't have one function to create or update, but need two separate ones.
Your update function would have to look something like this:
func updateMachineInRealm(machine: Machine){
var updatedMachine = Machine()
updatedMachine.name = machine.name
updatedMachine.value = machine.value + 42
updatedMachine.primaryKey = machine.primaryKey
// "transfer" or modify all the values of the old machine object
let realm = try! Realm()
do {
try realm.write() {
realm.add(updatedMachine, update: true)
}
}
Remember that you need a unique primary key for this code to work, because that is what Realm will match your new object with in the database.
Also, this of course has drawbacks when working on a bigger project with more than one thread accessing Realm objects etc. But it'll work for small projects (like I was working on and you seem to be working on).
这篇关于RealmSwift RLMException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!