Alexander Yakovlev
Alexander Yakovlev

Reputation: 510

Realm on iOS: crash while creating nested object with primaryKey

Can't create nested object with primaryKey: I have Media object with nested Location object and Comment object. In all these objects I've already implemented primaryKey and while I'm trying to create Media object, I get crash:

realm.add(media, update: true)

Can't create object with existing primary key value ...

By the way, the problem is with Comment object, but Location is ok, however, they are implemented similarly.

enter image description here

Upvotes: 1

Views: 454

Answers (1)

Alexander Yakovlev
Alexander Yakovlev

Reputation: 510

I've found the reason and it's in the wrong functions order. The right order is add Media to Realm and only then assign it to User:

realm.add(media, update: true)
currentUser.media.append(media)

Upvotes: 2

Related Questions