D
D
Denis2019-08-14 13:10:10
Android
Denis, 2019-08-14 13:10:10

Why can realm select results differ?

Hello!
There are two main places where Realm is used in my project. The base for almost all activity fragments and the service that is responsible for exchanging data with the server.
Faced the problem that the data written to the database in the activity may not be available in the service. At the same time, if you directly pull out the file of the database itself from the phone, then everything is written there correctly. The service itself also records everything correctly.
This happens if before the base activity, there was an authorization activity and a fragment located in it.
If there was an authorization activity, then the service is first launched from the authorization fragment, if not, then from the base activity.
I have the following template for reading and writing to the database:

abstract class DbRequest<T : RealmObject> {

    fun saveList(organisations: List<T>) = RealmConfigDB.getInstance().use {
        it.executeTransaction { realm ->
            organisations.forEach { realm.copyToRealmOrUpdate(it) }
        }
    }

    protected open fun select(namePrimaryKey: String, primaryKey: String, clazz: KClass<T>) = RealmConfigDB.getInstance().where(clazz.java).equalTo(namePrimaryKey, primaryKey).findFirst()?.let {
        it.realm.copyFromRealm(it)
    }

    protected fun selectAll(clazz: KClass<T>): MutableList<T>? = RealmConfigDB.getInstance().where(clazz.java).findAll()?.let {
        it.realm.copyFromRealm(it)
    }

    protected fun delete(namePrimaryKey: String, primaryKey: String, clazz: KClass<T>) = RealmConfigDB.getInstance().use {
        it.executeTransaction {
            it.where(clazz.java).equalTo(namePrimaryKey, primaryKey).findAll().deleteAllFromRealm()
        }
    }

    protected fun clearAll(clazz: KClass<T>) = RealmConfigDB.getInstance().use {
        it.executeTransaction {
            it.where(clazz.java).findAll().deleteAllFromRealm()
        }
    }

    protected fun selectList(nameKey: String, key: String, clazz: KClass<T>): MutableList<T>? = RealmConfigDB.getInstance().where(clazz.java).equalTo(nameKey, key).findAll()?.let {
        it.realm.copyFromRealm(it)
    }
}

interface DbRequestInterface {
    fun currentSave(tableObject: RealmObject) = RealmConfigDB.getInstance().use {
        it.executeTransaction {
            it.copyToRealmOrUpdate(tableObject)
        }
    }

    fun <T : RealmObject> currentDelete(namePrimaryKey: String, primaryKey: String, clazz: KClass<T>) = RealmConfigDB.getInstance().use {
        it.executeTransaction {
            it.where(clazz.java).equalTo(namePrimaryKey, primaryKey).findFirst()?.deleteFromRealm()
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2019-09-19
@lacredin

As aelimill noted in the comments , you need to close the work with eralm after performing any operation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question