1

I need to track only Realm INSERT operations. Is there a listener for something like this?

I use a Realm-java for Android and work with a server that sends data in independent parts. For example, a Person or a Pet can be received via websocket in any order. For example, the server can first send me a Pet and then after a few minutes a Person. Or vice versa. I can't control it. I want to save data from the server to the database without any logic. At the same time, the listener, who reacts ONLY to the insertion, puts a link to the Pet in the Person after the recording. For example, it connects a newly received Person with a Pet already in the database, whose ownerId is equal to the inserted Person's id.

1 Answers1

0

I found a solution: to track only the insert, you need to use DynamicRealm and OrderedRealmCollectionChangeListener on the search result.

val dRealm = DynamicRealm.getInstance(conf)
dRealm
    .where('Person')
    .findAllAsync()
    .apply {
        addChangeListener { results: RealmResults<DynamicRealmObject>, changeSet: OrderedCollectionChangeSet ->
            if (changeSet.insertions.isNotEmpty()) {
                // do what you need
            }
        }
    }
    .asFlowable()
    .subscribe()