composite primary key realm/swift

Related searches

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 ?

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.

Composite @PrimaryKey from two fields � Issue #6108 � realm/realm , Here my java pojo in my Android project: public class Product extends RealmObject { @PrimaryKey private int id; private String name; private� Please note that beginning from Realm Swift 1.0.1, you can't use lazy properties anymore due to interoperability issues with the Objective-C runtime as seen in #3337. That means if you declared your compound key variable as lazy , you will need to drop this attribute from your property declaration after updating.

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

Compound Key � Issue #1192 � realm/realm-cocoa � GitHub, It is possible to have a compound key or a composite key (a primary key using this on managed properties from Realm Swift is now explicitly� Primary keys. Override Object.primaryKey() to set the model’s primary key. Declaring a primary key allows objects to be looked up and updated efficiently and enforces uniqueness for each value. Once an object with a primary key is added to a Realm, the primary key cannot be changed.

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

Realm Primary Keys Tutorial, Realm primary keys allow granular fetching of Realm objects – See how easy they Swift: class Person: Object { dynamic var personID = UUID(). That being said, if your app maintains multiple Realm files, and you want to� Creates or updates a Realm object with a given value, adding it to the Realm and returning it. You may only pass true to update if the object has a primary key. If no object exists in the Realm with the same primary key value, the object is inserted. Otherwise, the existing object is updated with any changed values.

composite primary key realm/swift, 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� Realm should also support composite primary keys. Reusing the @PrimaryKey annotation should be relatively simple. Primary keys are currently implemented in the binding. This should be implemented at the core level: realm/realm-core#831.

If your objects can have primary keys then you can utilize Realm's merge logic to achieve the goal. Another neat feature of Realm is that you can create objects on two devices with the same primary key and they will ultimately merge once synced into just having one object. The only thing to keep in mind is how the merge of these two objects happen.

So to force ourselves into the good habit of declaring primary key, we can leverage Swift protocol Create primary constrain protocol like this protocol PrimaryKeyAware { var id : Int { get } static func primaryKey () -> String ?

Comments
  • if we declare composite key variable like this: dynamic lazy var fullName: String = "(self.id)-(self.tourId)" then what is problem in that ? Thanks in advance
  • Swift lazy properties are explicitly forbidden, unless they are ignored. How do we solve this issue now @Michal?
  • I got Terminating app due to uncaught exception '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.'
  • @BlackTiger The only way is to channel the writes to id and tourId only through setCompoundID(id: Int) and setCompoundTourId(tourId: Int) functions, so you know for sure that a latest compoundKey has been written, then you can save the realm. Also if you choose to implement things this way, then you can remove lazy keyword and self.compoundKeyValue()
  • Hi, I am getting "Terminating app due to uncaught exception '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." on lazy compound key.
  • sorry bud, but we already know workarounds ( as in your answer ), but the discussion about native support of compound keys has been going on for some time and the issue is tracked here : github.com/realm/realm-cocoa/issues/3453