List property becomes empty when multiple clients write to realm at the same time

Hi everyone,

I’ve been having this problem for a while. I have my subclass of Object set up with a List property that stores another type of Object that I have defined. I’ll use this definition as an example.

class A: Object {
    @objc dynamic var id: String
    @objc dynamic var property1: String
    @objc dynamic var property2: String
    let items: List<B> = List<B>()

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

I have set up my realm to connect Realm Cloud so that I can listen for changes in the database on the client and update the UI accordingly. Now, whenever I write A to the realm using one client at a time, everything works fine: the A object on the other clients gets updated and the UI refreshes, reflecting the new changes.

However, when I perform write operations on the same A Object using two clients at the same time (or within a very very short amount of time), the List property becomes empty (items in the example above). When I check Realm Studio, I can see that the items property of A is an empty array. However, the actual items themselves have not been deleted. i.e. when I look up B in Realm Studio, the individual instances of B are there, but they are not in the List in A. In other words, the references to all the B objects have disappeared.

Why does this happen? Is it something about writing concurrently to the same object in the realm? I tried finding documentation on conflict resolution and what the behaviour for writing the same object from different clients is, but I couldn’t find anything.

I’ve been using add(:update:) to write to the realm. I saw that I can also use create(:value:update:). Should I be using this instead? Could that be the problem?

Would really appreciate any help on this. I’ve been stuck on this for ages and it’s been annoying me. I’ve been having to go into Realm Studio and manually restore all the references of B to A.

Many thanks,
Aero

Can you try it out without subclassing the realm object?
Realm doesnt like subclassing :slight_smile:

Sorry, I had the class definition wrong. Just updated it.

What do you mean? You’re supposed to subclass Object

Hmm, now it seems like good to me.
I have a few classes like this, but i am not tested with two clients at once (its not my priority).

Now i think i should… :smiley:

Do you have inverse relationships on the class B ?

Oh, one more: i think you should not use @objc on the primaryKey definition.

Did you ever solve this ? Have you tried using LinkingObjects rather than List to maintain the relationships?

I have the exact same problem. Is there any solution for this?

It does not only reset the list where the conflict happens, but all lists of the entry.
In my opinion this is a major bug for a synced database!

@Realm Are you already investigating this?

By the way, I’m using RealmJS, so this is not just a swift problem. It seems the merge algorithm is buggy!

The merge algorithm is well tested and reasonably bug-free. If you can provide a repro case, I’m sure the team will be interested in helping identify the issue.

@durac @aero I tried (hard - but failed) to reproduce this issue. I would really appreciate if either of you could find the time to create a public GitHub repository with an app displaying this behavior.