Accessing Changed Objects Directly?


I’ve been finding a service that I can hook up my app to do syncing and came across Realm Sync.

While I’m following the tutorial, referencing the example codes, documentation, and getting comfortable with Realm Sync, I couldn’t find out one thing.

When I am deleting an object, I want to see what object has been deleted.

In order to do that, I’ve been following the example code in the documentation.

It’s a bit embarrassing, but I’ve been logging every one of methods to see what exactly they are returning.

Here is what I am doing below.

val allQuery = realm.query<realmNote>(realmNote::class)
            val allQueryFlow = allQuery.asFlow()
            var initial = listOf<realmNote>()
            allQueryFlow.collect() {
                    changes: ResultsChange<realmNote> ->
                    when (changes) {
                        is InitialResults -> {
                            Log.i("realm initial", "${changes.list}")
                            initial = changes.list
                        // UpdatedResults means this change represents an update/insert/delete operation
                        is UpdatedResults -> {
                            changes.insertions // indexes of inserted objects
                            changes.insertionRanges // ranges of inserted objects
                            changes.changes // indexes of modified objects
                            changes.changeRanges // ranges of modified objects
                            changes.deletions // indexes of deleted objects
                            changes.deletionRanges // ranges of deleted objects

                            Log.i("realm insertions", "${changes.insertions.size}")
                            Log.i("realm insertion ranges", changes.insertionRanges.size.toString())
                            Log.i("realm changes", "${changes.changes.size}")
                            Log.i("realm change ranges", changes.changeRanges.size.toString())
                            Log.i("realm deletions", "${changes.deletions.size}")
                            Log.i("realm deletion ranges", changes.deletionRanges.size.toString())
                            Log.i("realm full collection", "${changes.list}") // the full collection of objects
                            if (changes.deletions.isNotEmpty()) {
                                for (del in changes.deletions) {
                                    Log.i("realm dd ranges", "$del")
                                    Log.i("realm dd obj", initial[del].name)
//                                    for (i in start until end ) {
//                                        Log.i(
//                                            "realm deletion obj",
//                                            changes.list[changes.deletions[i]].name
//                                        )
//                                    }
                        else -> {
                            // types other than UpdatedResults are not changes -- ignore them

I’ve found that almost all the example apps use recylcerviews and thus use the range methods directly.

What I was trying to do up there is to get the comparison of the InitialResults and UpdatedResults in actual objects, not indices.

Is there any method that does that in Realm Sync? Am I missing something from the documents?

I’m trying to make a simple multi-player game where players each have objects in their local storage (In an object array and hashmap) first, and if a deletion occurs, that local array needs to be updated.

I want to pinpoint the deleted, or updated object by their id and apply the changes to the local storage.

The reason why I can’t use the returned indices is because I cannot guarantee that the local array will always have the same ordering as the ordering of the server.

Also, updating the local array entirely each time a syncing happens sounds too expensive.

All in all, right now I have to access the InitalResults using the indices returned by UpdatedResults. Also, if any insertion occurs, I’d have to add those elements in InitialResults since InitialResults seems to be called only once in the very beginning of the query.

Any way I can do this with Realm Sync provided methods?

Thank you for taking your time to read the post.