Reputation: 3579
I have two places (threads) where I can create/change realm objects. From both places I can have simultaneous request to create same object. That should be unique and added just once to some relation.
I can see that if I want to create something just once I need to use add(_:update:)
or create(_:value:update:)
api. But I don't see any way to put object just once to relationship.
Upvotes: 0
Views: 182
Reputation: 7806
add(_:update:)
with update: true
and its friends take care of updating related objects as well. Make sure that those have also primary keys, otherwise you'd end up inevitable with duplicates.
Beside that, you could just check for the presence of a related object in a write transaction, after creating the root object with one of these methods. Write transactions are using a locking mechanism across all opened Realm for the same file, so you don't have to worry about simultaneously putting an object in a relationship as long as you check within the write transaction itself, whether it is not already part of it.
Upvotes: 1