Creating two objects A and B with a dependency of B on A is immediately reverted once synced

cloud

#1

Hello,

With the latest Realm Cocoa in Swift + Realm Cloud. I am writing two new objects Person and Dog in one write transaction to a partially synced Realms where Dog has a property of type Person. I discovered that, if that Person type property on Dog is the one that I created in the same write transaction, aka has not synced to the cloud yet, then the creation of Dog is immediately reverted once synced. And it has no error log on server or client side.
If I create a Dog with an existing/synced Person then everything works.
This is an expected behavior? If so how do I suppose to create two new objects with a dependency like this?

Thanks a bunch!


#2

How are you trying to save the Dog object with the Person?
You are only saving the Dog, right?


#3

Hello!
I am saving both the Dog and Person, like this:
realm.write {
let person: [String: Any] = [“identity”: personID]
realm.create(Person.self, value: person, update: .modified)
let dog: [String: Any] = [“identity”: dogID, “owner”: [“identity”: personID]]
realm.create(Dog.self, value: dog, update: .modified)
}
In this write the person will be created and synced, that dog can be locally created, but is immediately reverted once synced, I think the factor here is that owner personID has not been synced yet. If I break it into two writes and let it wait for 1 second after Person is created, then it would work.


#4

You dont need to add the person to the realm first - it will be added when you add the Dog.


#5

Haha Yes! that solves it. Thank you so much for the help!