具有惰性 属性 的 Realm 中的复合键
Compound key in Realm with lazy property
我在 Swift 中找到了将 Realm 与复合主键结合使用的绝佳解决方案:https://github.com/realm/realm-cocoa/issues/1192
public final class Card: Object {
public dynamic var id = 0 {
didSet {
compoundKey = compoundKeyValue()
}
}
public dynamic var type = "" {
didSet {
compoundKey = compoundKeyValue()
}
}
public dynamic lazy var compoundKey: String = self.compoundKeyValue()
public override static func primaryKey() -> String? {
return "compoundKey"
}
private func compoundKeyValue() -> String {
return "\(id)-\(type)"
}
}
但是我发现 Realm 不支持惰性属性,在我的例子中我收到了这个错误:
exception NSException * name: "RLMException" - reason: "Lazy managed property 'compoundKey' is not allowed on a Realm Swift object class. Either add the property to the ignored properties list or make it non-lazy." 0x00007f8a05108060
是否仍然可以使用没有惰性的复合键属性?
您找到的解决方案已过时。我会在那里留下一张纸条。我建议删除 lazy
修饰符并将 compoundKey
初始化为空字符串。
我在 Swift 中找到了将 Realm 与复合主键结合使用的绝佳解决方案:https://github.com/realm/realm-cocoa/issues/1192
public final class Card: Object {
public dynamic var id = 0 {
didSet {
compoundKey = compoundKeyValue()
}
}
public dynamic var type = "" {
didSet {
compoundKey = compoundKeyValue()
}
}
public dynamic lazy var compoundKey: String = self.compoundKeyValue()
public override static func primaryKey() -> String? {
return "compoundKey"
}
private func compoundKeyValue() -> String {
return "\(id)-\(type)"
}
}
但是我发现 Realm 不支持惰性属性,在我的例子中我收到了这个错误:
exception NSException * name: "RLMException" - reason: "Lazy managed property 'compoundKey' is not allowed on a Realm Swift object class. Either add the property to the ignored properties list or make it non-lazy." 0x00007f8a05108060
是否仍然可以使用没有惰性的复合键属性?
您找到的解决方案已过时。我会在那里留下一张纸条。我建议删除 lazy
修饰符并将 compoundKey
初始化为空字符串。