如果我以相同的方法执行两个事务,则领域已处于写事务中

时间:2019-02-06 07:46:11

标签: android database kotlin concurrency realm

如果它们超出一定数量,我需要消除领域db记录的第一条记录,但是我遇到了这个异常:

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)````

如下所示,我尝试添加commitTransaction,玩executeTransactionAsyncexecuteTransaction,开始交易等。尽管我读了几篇这样的文章,但没有任何效果和GitHub发布评论,我什至post都这么认为,但是太抽象了,无法与我的案例联系起来

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

根据建议,我将消除beginTransaction()commitTransaction,我也改为了executeTransaction而不是`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()

        }

    }

现在我知道,就像Realm取消第一个0位置,然后将下一个记录添加到0位置而不是列表的结尾

如果我取消了第二个块executeTransaction,则按顺序成功添加了记录,但保留了第二个块,则事务消除了行0,但随后在位置0而不是末尾插入了第二条记录列表

2 个答案:

答案 0 :(得分:1)

如果您使用executeTransaction,则无需手动编写beginTransaction()commitTransaction()即可为您完成所有操作。  因此,基本上,您beginTransaction()然后开始executeTransaction块,然后在事务内部获取事务,这就是崩溃的原因,您也可以在一个事务中完成所有这些代码。

答案 1 :(得分:1)

如果我正确理解了您的问题,则要消除/删除前两个记录(如果有),然后在列表末尾插入新记录。 尝试

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)
            }
        }
    }
}

如果我理解错了,请纠正我。