I'm new to swift and realm. I want to make a composite primary key and when I'm trying something like this :
class DbLocation : Object {
dynamic var id = 0
dynamic var tourId = 0
dynamic var uuid : String {
return "\(id)\(tourId)"
}
override static func primaryKey() -> String? {
return "uuid"
}
}
I'm getting this error : 'Primary key property 'uuid' does not exist on object 'DbLocation'
Anyone can help me out with an example how to create a composite primary key ?
This should give you the answer:
class DbLocation: Object {
dynamic var id = 0
dynamic var tourId = 0
func setCompoundID(id: Int) {
self.id = id
compoundKey = compoundKeyValue()
}
func setCompoundTourId(tourId: Int) {
self.tourId = tourId
compoundKey = compoundKeyValue()
}
dynamic lazy var compoundKey: String = self.compoundKeyValue()
override static func primaryKey() -> String? {
return "compoundKey"
}
func compoundKeyValue() -> String {
return "\(id)\(tourId)"
}
}
The custom setters make sure, that the compoundKey is always updated, the lazy key word makes sure that the first time you access it, it will be derived from what you've already set.
Find out more on this topic in this thread where this issue has been debated.
Simple create a new property whose value is set to interested other properties which you expect to be compound primary keys.
class DbLocation: Object {
dynamic var id = 0
dynamic var tourId = 0
dynamic var compoundKey: String? = ""
override static func primaryKey() -> String? {
return "compoundKey"
}
}
let location = DbLocation()
location.tourId = 1
location.id = 5
location.compoundKey = "\(id) \(tourId)"
For latest version of Swift and Realm, I would do something like this.
dynamic private var compoundKey: String = ""
required convenience init?(map: Map) {
self.init()
if let firstValue = map.JSON["firstValue"] as? String,
let secondValue = map.JSON["secondValue"] as? Int {
compoundKey = firstValue + "|someStringToDistinguish|" + "\(secondValue)"
}
}
来源:https://stackoverflow.com/questions/31265666/composite-primary-key-realm-swift