composite primary key realm/swift

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.


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

Tags:

Ios

Swift

Realm