vishalaksh
vishalaksh

Reputation: 2154

How to set attribute of member object of a realm model?

I have a realm object called DiscoverUserInfo:

class DiscoverUserInfo: Object , Mappable{

    dynamic var UserObject:User?
    dynamic var ConnectionStatus:Int = -1
    var PreviousMeetings = List<Meeting>()

    required convenience init?(map: Map) {
        self.init()
    }

    override class func primaryKey() -> String? { return "UserObject.UserId" }

}

Now for this, I want to set a primary key which is UserId of UserObject.

But when I run this code, I get this error:

Terminating app due to uncaught exception 'RLMException', reason: 'Primary key property 'UserObject.UserId' does not exist on object 'DiscoverUserInfo''

Upvotes: 0

Views: 231

Answers (1)

Faris Sbahi
Faris Sbahi

Reputation: 666

You cannot set a primary key using a property of a dynamic variable. You'll have to do something like this:

class DiscoverUserInfo: Object , Mappable{

    dynamic var UserObject: User?
    dynamic var id = ""
    dynamic var ConnectionStatus:Int = -1
    var PreviousMeetings = List<Meeting>()

    required convenience init?(map: Map) {
    self.init()
     }

override class func primaryKey() -> String? { 
     return "id" 
     }

}

and then set the id to the associated UserObject's UserId each time you create a new DiscoverUserInfo object.

This is related to the issue of having no native support for compound primary keys in Realm. However, we expect to see this feature down the road.

Upvotes: 0

Related Questions