Help with updating object

I try to update an object with:

    fun checkEmbeddedPicture() {
        realm.writeBlocking {
            findLatest(this@FeedMedia)?.let {
                it.file_url = "new/file/location"
            }
        }
        Log.d(TAG, file_url)
    }

I presume the object is update in the DB, but the Log statement shows the version in memory is not update. How can I update the in-memory version of the object?

Hi @X_Jia.

It is unclear what file_url is referring to. Objects retrieved from a realm will reference the snapshot as of the time it is obtained. If you want to reference the new updated value from the realm you will have to return the instance from the writeBlocking-block, query it again or register a listener flow:

val updatedInstance = realm.writeBlocking {
    findLatest(this@FeedMedia)?.let { }
} 
// updatedInstance.file_url now holds the updated value from the instance in the database

// New query
realm.query<...>(query-filter)

// Notification flow. Each item will reference the updated instance
this@FeedMedia.asFlow().collect { objectChange -> 
   objectChange.obj.file_url 
}

… Claus

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.