gpt4 book ai didi

android - Realm Kotlin - 删除 Realm 对象

转载 作者:行者123 更新时间:2023-12-05 05:42:37 24 4
gpt4 key购买 nike

https://www.mongodb.com/docs/realm/sdk/kotlin/realm-database/delete/delete-all-objects-of-a-type/

我正在我的项目中学习新的 kotlin-realm。但我不知道如何删除对象。它一直显示错误 Caused by: io.realm.internal.interop.RealmCoreNotInATransactionException: [5]: Must be in a write transaction

这是代码:

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val realm by lazy { (application as CustomApplication).realm }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

realm.writeBlocking {
copyToRealm(Buku(
name = "Perjuangan Menjual Baju"
))
copyToRealm(Buku(
name = "Perang Saudara"
))
}

val buku = realm.query<Buku>("name BEGINSWITH $0", "pera")
Log.i("AOEU", "buku = $buku")

CoroutineScope(Dispatchers.Main).launch {
val query = realm.query<Buku>().find()
realm.write {
delete(query)
}
}
}
}

最佳答案

经过几天的寻找解决方案。我刚刚意识到我的代码中唯一的错误是

val query = realm.query().find()

应该替换为

val query = this.query().find()

关于android - Realm Kotlin - 删除 Realm 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71997587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com