简体   繁体   中英

kotlin android get deleted value from realm-java

I use realm-java on Android. now I'm creating profile function, but I'm not sure how to user realm correctly.

when renew profile, delete value -> store value but, I fetch value from realm, sometimes old value is taken.

To reproduce, My test repository is below, and I attached movie that problem is reproduced. https://github.com/shinsan/realm_test/

When thread id is changed, sometimes old value appears. so, if you try to reproduce, please use lower memory device such as nexus5 simulator #I think Realm instance is singleton and transaction is thread-safe, so value is always only one.

my code kotlin + Android Studio Realm Java 10.3

//Store

val realm = Realm.getDefaultInstance()
realm.executeTransaction {
val entity = AccountProfileEntity(accountProfile)
it.copyToRealmOrUpdate(entity)
}

//Delete

val realm = Realm.getDefaultInstance()
val entity = realm.where(AccountProfileEntity::class.java).findFirst()
realm.executeTransaction {
entity?.deleteFromRealm()
}

//Fetch

val realm = Realm.getDefaultInstance()
val instance = realm.where(AccountProfileEntity::class.java).findFirst()
return instance?.toModel()

// profile get function

override suspend fun getProfile(isForce: Boolean): AccountProfile =
withContext(Dispatchers.IO) {
if (isForce) {
database.delete()
}

val profile = database.fetch()
if (profile != null) {
return@withContext profile
}

val token = prefs.getToken() ?: throw NoTokenException
val response = service.getProfile(token)
database.store(response)
response
}

Please Help

I solved this. simply I forgot to close realm instance each fetch, store, delete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM