Tracking and Identifying Realm Object Deletions


#1

Hello,

I’m building an iOS app and I’m interested in backing-up and synchronizing my local Realm database to my own server. I need to track insertions, deletions, and updates and mirror those on my server.

Collection Notifications seem like the place to build this integration, but I can’t figure out how to sync deletions because RLMResults objects are live. By the time the notification is triggered, the items that were deleted are already inaccessible, and the RLMCollectionChange object only contains the indices of the objects before they got deleted. It seems to me that to get a reference to the deleted objects themselves (at least their primaryKey), I have to keep my own, non-live version of the RLMResult and keep that in sync with the RLMResults as well, which seems like a totally incorrect approach.

I can see that I can add an Object Notification as well, and that will let me know when a specific RLMObject is deleted, but adding a notification for each and every object in my Realm also seems inefficient. Is that not the case?

I just started researching this and I’ve done my fair share of Googling to no avail. It’s entirely possible I missed something obvious about the solution.


#2

Doing some further research, I see Realm Swift samples that track deletions using the following snippet:

notificationToken = notes.addNotificationBlock { [weak self] changes in
    guard let welf = self else { return }
        
    switch changes {
    case .update(let collection, _, let insertions, let modifications):
      let notesToDelete = modifications.map({ collection[$0] }).filter({ $0.isDeleted })
      ...

The snippets seem to indicate that the notification includes deleted items in the “modifications” list of indices, and they’re identified using the “isDeleted” property. However, in Realm Objective-C, the RLMCollectionChange object’s modifications property doesn’t include deleted items, nor does the results RLMResults property passed in.

Is this possible in Realm Swift but not Realm Objective-C?


#3

This issue seems to be sharing my concern: https://github.com/realm/realm-java/issues/4398