I'm using Realm for Swift 1.2, and I'm wondering how to implement a compound primary key for an entity.
So you specify your primary key by overriding primaryKey()
override static func primaryKey() -> String? { // <--- only 1 field
return "id"
}
The only way I can see is to create another compound attribute like so
var key1 = "unique thing"
var key2 = 123012
lazy var key: String? = {
return "\(self.key1)\(self.key2)"
}()
override static func primaryKey() -> String? {
return "key"
}
How do you properly supply compound keys in Realm?
A compound primary key is made up of multiple fields and follows the requirements described in Primary Keys in Entities. To use a compound primary key, you must create a wrapper class. In order, two entities use compound primary keys: Part and LineItem .
In database design, a composite key is a candidate key that consists of two or more attributes (table columns) that together uniquely identify an entity occurrence (table row). A compound key is a composite key for which each attribute that makes up the key is a foreign key in its own right.
On the view controller write a simple function to get the “timeIntervalSince1970″, which is a built-in function with swift. You also need to import the Realm Framework on the view controller too. And finally update the unique ID to Realm.
Primary key values are immutable. To change the primary key value of an object, you must delete the original object and insert a new object with a different primary key value. You cannot change the primary key field for an object type after adding any object of that type to a realm.
It appears that is the correct way to return a compound key in Realm.
Here's the answer from Realm : https://github.com/realm/realm-cocoa/issues/1192
You could use a mix of lazy and didSet instead to have the compoundKey property be both derived and stored:
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)" } } // Example let card = Card() card.id = 42 card.type = "yolo" card.compoundKey // => "42-yolo"
The correct answer needs an update (regarding didSet and lazy)
Taken from "mrackwitz" at Realm git-hub: https://github.com/realm/realm-cocoa/issues/1192 :
public final class Card: Object {
public dynamic var id = 0
public func setCompoundID(id: Int) {
self.id = id
compoundKey = compoundKeyValue()
}
public dynamic var type = ""
public func setCompoundType(type: String) {
self.type = type
compoundKey = compoundKeyValue()
}
public dynamic var compoundKey: String = "0-"
public override static func primaryKey() -> String? {
return "compoundKey"
}
private func compoundKeyValue() -> String {
return "\(id)-\(type)"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With