Realm on iOS: crash while creating nested object with primaryKey

时间:2017-07-12 08:12:32

标签: ios swift realm primary-key

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

1 个答案:

答案 0 :(得分:2)

我找到了原因并且它的功能顺序错误。正确的顺序是将Media添加到Realm,然后将其分配给User:

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