Wednesday, 17 August 2022

Realm Kotlin - Delete realm object

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

I am learning new kotlin-realm in my project. But i dont know how to delete objects. It keep showing error Caused by: io.realm.internal.interop.RealmCoreNotInATransactionException: [5]: Must be in a write transaction

this is the code :

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


from Realm Kotlin - Delete realm object

No comments:

Post a Comment