composite primary key realm/swift

2020-02-23 06:28发布

问题:

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 ?

回答1:

For 1.0.1+ of Realm:

class DbLocation: Object{
    dynamic var id = 0
    dynamic var tourId = 0
    dynamic var compoundKey = ""

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

    func setup(id: Int, tourId: Int){
        self.id = id
        self.tourId = tourId
        self.compoundKey = compoundKeyValue()
    }

    func compoundKeyValue() -> String {
        return "\(id)\(tourId)"
    }
}

Usage example:

let location = DbLocation()
location.setup(id: 0, tourId: 1) 
print(location.compoundKey) // "01"

Of course you can play around with using various didSet listeners on id and tourId, to make sure that compoundKey gets properly rewritten every time the values get changed.

For pre-1.0.1 of Realm:

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.



回答2:

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)"


回答3:

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)"
  }
}


标签: ios swift realm