Ad

Realm Is Already In A Write Transaction If I Execute Two Transactions In The Same Method

- 1 answer

I need to eliminate the first records recorded by the realm db if they are more than a certain number, but I get this exception:

Caused by: java.lang.IllegalStateException: The Realm is already in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsSharedRealm.cpp line 126
    at io.realm.internal.OsSharedRealm.nativeBeginTransaction(Native Method)
    at io.realm.internal.OsSharedRealm.beginTransaction(OsSharedRealm.java:269)
    at io.realm.BaseRealm.beginTransaction(BaseRealm.java:404)
    at io.realm.Realm.beginTransaction(Realm.java:145)
    at io.realm.Realm.executeTransaction(Realm.java:1491)
    atsavedAddresses(AddressBook.kt:39)````

As you can see below I tried to add commitTransaction, to play with executeTransactionAsync, and executeTransaction, begin transaction etc. But nothing is going to work although I read several so posts and GitHub issues comments , I even found this so post but is too abstract to relate to my case

init {

    Realm.init(ProActiveApplication.applicationContext())
}


private val realm = Realm.getDefaultInstance()

val query = realm.where(RecentAddress::class.java).findAll()

fun savedAddresses(name: String, address: String) {
   realm.beginTransaction()
    realm.executeTransactionAsync  {
        val addressToSave = realm.createObject(RecentAddress::class.java)
        addressToSave.name = name
        addressToSave.street = address
        Log.d("ME","-----------------")
        Log.d("ME", " CreateTransaction: ${addressToSave.name} in ${addressToSave.street}")
        Log.d("ME","-----------------")
    }
    realm.commitTransaction()
    realm.close()

    realm.beginTransaction()
    realm.executeTransaction {
        if (query.size > 2) {
            val location = query.get(0)
            Log.d("ME","-----------------")

            Log.d("ME", " This is cancelled: ${query.get(0)}")
            Log.d("ME","-----------------")

            location?.deleteFromRealm()

        } }

EDIT

As suggested I am going to eliminate beginTransaction() and commitTransaction also I changed to executeTransaction instead of `executeTransactionAsync()

    fun savedAddresses(name: String, address: String) {
   // realm.beginTransaction()
    realm.executeTransaction {
        val addressToSave = realm.createObject(RecentAddress::class.java)
        addressToSave.name = name
        addressToSave.street = address
        Log.d("ME", "-----------------")
        Log.d("ME", " CreateTransaction: ${addressToSave.name} in ${addressToSave.street}")
        Log.d("ME", "-----------------")
    }
   // realm.commitTransaction()
    realm.close()

    // realm.beginTransaction()
    realm.executeTransaction {
        if (query.size > 2) {
            val location = query.get(0)
            Log.d("ME", "-----------------")

            Log.d("ME", " This is cancelled: ${query.get(0)}")
            Log.d("ME", "-----------------")

            location?.deleteFromRealm()

        }

    }

Now I get that is like Realm is going to cancel the first 0 position, but then is going to add the next record in the 0 position and not at end of the list

If I cancel the second block executeTransaction the record are successfully added in sequential order, but remaining the second block, the transaction eliminate row 0 but then insert the second record at the position 0, not at the end of the list

Ad

Answer

If I understand your problem correctly, you want to eliminate/remove first two records (if there are) and insert new record at the end of the list. Try this

fun savedAddresses(name: String, address: String) {
   realm.executeTransaction { realm ->
        val addressToSave = realm.createObject(RecentAddress::class.java)
        addressToSave.name = name
        addressToSave.street = address
        //insert the record at the end of the list 
        realm.insert(addressToSave)
    }
    deleteOldRecords()
}

fun deleteOldRecords() {
    realm.executeTransaction { realm -> 
        val recentAddressList = realm.where(RecentAddress::class.java).findAll()
        if(recentAddressList.size > 2){
            for(i in 0..1) {
                recentAddressList.deleteFromRealm(i)
            }
        }
    }
}

please correct me if i understood wrong.

Ad
source: stackoverflow.com
Ad