【问题标题】:Realm Adding Object with Compound primaryKey Error领域添加具有复合主键错误的对象
【发布时间】:2016-07-01 16:37:36
【问题描述】:

我有一个简单的课程

class FarmRecord: Object {
    dynamic var year = ""
    dynamic var month = ""
    dynamic var day = ""

    func setYearID(inYear: String) {
        self.year = inYear
        compoundKey = compoundKeyValue()
    }
    func setMonthID(inMonth: String) {
        self.month = inMonth
        compoundKey = compoundKeyValue()
    }
    func setDayID(inDay: String) {
        self.day = inDay
        compoundKey = compoundKeyValue()
    }


    dynamic lazy var compoundKey: String = self.compoundKeyValue()

    private func compoundKeyValue() -> String {
        return "\(year)\(month)\(day)"
    }

    override static func primaryKey() -> String? {
        return "compoundKey"
    }
}

我尝试添加对象如下:

let storeRealm = try! Realm()
let farm = FarmRecord()
farm.setYearID("2016")
farm.setMonthID("3")
farm.setDayID("1")
do {
    try storeRealm.write {
        storeRealm.add(farm)
    }
} catch {
}

我看到EXEC_BAD_ACCESS (code = 1) 发生崩溃。我什至试过storeRealm.add(farm, update: true),没有任何区别。

【问题讨论】:

    标签: swift primary-key realm


    【解决方案1】:

    Realm 似乎错误地处理了您的 compoundKey 属性,因为它被标记为 lazy。我写了一个bug report about the issue on GitHub。作为一种解决方法,我建议删除 lazy 修饰符并将 compoundKey 初始化为空字符串。

    【讨论】:

    • 感谢您的快速答复。照你说的做就行了。我现在可以添加对象了。
    • 我刚刚遇到了这个问题。我实际上不需要存储属性并将其添加到ignoreProperties。我仍然遇到同样的崩溃。
    • @DanielWood,请在Realm GitHub repository 中提出问题,以便我们帮助您了解您所看到的内容并为您提供解决方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多