我有一个简单的课程
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)
没有区别。
答案 0 :(得分:1)
Realm似乎处理不当compoundKey
属性,因为它被标记为lazy
。我写了bug report about the issue on GitHub。作为一种解决方法,我建议删除lazy
修饰符并将compoundKey
初始化为空字符串。