尝试快速更新领域对象时获取SIGABRT

用户名

警告:我是iOS,Swift和Realm的新手。使用Realm保存和检索时没有问题,但似乎无法崩溃就不会更新现有对象。

AppDelegate:

class Bale: Object {
    dynamic var uid = NSUUID().UUIDString
    dynamic var id = 0
    dynamic var number = 0
    dynamic var type = 0
    dynamic var weight = 0
    dynamic var size = ""
    dynamic var notes = ""
    override static func primaryKey() -> String? {
        return "uid"
    }
}

在其他地方:(xcode坚持所有!)

    let bale: Bale = getBaleByIndex(baleSelected)
    bale.id = Int(textID.text!)!
    bale.number = Int(textNumber.text!)!
    bale.type = Int(textType.text!)!
    bale.weight = Int(textWeight.text!)!
    bale.size = textSize.text!
    bale.notes = textNotes.text!

    try! realm.write {
        realm.add(bale, update: true)
    }

getBaleByIndex:

func getBaleByIndex(index: Int) -> Bale {
    return bales[index]
}

我从其他地方从getBaleByIndex返回的Bale对象读取数据,因此该函数可以正常工作。我在类AppDelegate上得到了SIGABRT :UIResponder,UIApplicationDelegate {在领域文档或示例中没有完整的示例显示更新。我也尝试过使用realm.create和适当的参数,但还是不行。它看起来很简单,所以我确定我正在做一些愚蠢的事情。任何帮助都将是极好的。谢谢!

马里乌斯

在这里困扰您的是,一旦您将一个对象添加到Realm中,数据就不仅仅存储在内存中,而是直接存储在持久性存储中。您必须在写事务中对您的对象进行所有修改,并且在提交写事务后它们将自动生效。如果以前已将其持久化,则无需再次将其添加到领域。因此,您需要将代码更改为以下内容:

try! realm.write {
    let bale: Bale = getBaleByIndex(baleSelected)
    bale.id = Int(textID.text!)!
    bale.number = Int(textNumber.text!)!
    bale.type = Int(textType.text!)!
    bale.weight = Int(textWeight.text!)!
    bale.size = textSize.text!
    bale.notes = textNotes.text!

    // Not needed, but depends on the implementation of `getBaleByIndex`
    // and whether there is the guarantee that it always returns already
    // persisted objects.
    //realm.add(bale, update: true)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章