question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong transactional state when trying to call .delete()

See original GitHub issue

first I have to say this version is really better than Java. I love it that I dont have to care about which thread will call Realm, this is perfect for my small project to practice.

so im having questions about .delete() function, here is my test that is failing

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.realm.Realm
import io.realm.RealmConfiguration
import io.realm.RealmObject
import io.realm.delete
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class RepoTest {

    class Task : RealmObject {
        var name: String = "new task"
        var status: String = "Open"
    }

    class TaskRepository {

        private val config = RealmConfiguration(schema = setOf(Task::class))
        private val realm = Realm(config)

        init {
            realm.writeBlocking {
                objects(Task::class).delete()
            }
        }

        fun getAll(): List<Task> {
            return realm.objects(Task::class)
        }

        fun addTask(task: Task): Task {
            return realm.writeBlocking {
                copyToRealm(task)
            }
        }

        fun deleteTask(task: Task) {
            realm.writeBlocking {
//                findLatest(task)?.delete() // this works
//                delete(task) // why it does not work with this
                task.delete() // and this also does not work
            }
        }
    }

    private val myRepo = TaskRepository()


    @Test
    fun testRepo() {
        val myTask = Task().apply {
            name = "old task"
        }

        val mySavedTask = myRepo.addTask(myTask)

        myRepo.deleteTask(mySavedTask)

        assert(myRepo.getAll().isEmpty())

    }

}

I have created a deleteTask function, but both delete(task) and task.delete() doesnot work. only findLatest(task)?.delete() works, but why and whats the correct way to use .delete()?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rorbechcommented, Aug 17, 2021

Thanks for the feedback.

A side effect of the new approach with immutable frozen objects is that you have to obtain a reference to the object within the write transaction with findLatest. For now the various calls (object.delete() and delete(object)) act directly on the version of the object passed to the methods, so you cannot delete the frozen instances.

For MutableRealm.delete(object) we could probably just improve this by internally resolving the latest version, but as RealmObject.delete() is already an extension method we cannot get the scope of the MutableRealm so not sure there is an easy fix for that.

Anyway, as you already have discovered the right way is to obtain a reference to the object within the write transaction by findLatest and call any of the delete methods with that reference.

0reactions
rorbechcommented, Aug 27, 2021

Will close this one as overcoming the erroneous transactional state is fixed by getting a reference to the live object with findLatest. So deletion can be done with:

realm.writeBlocking {
        findLatest(task)?.delete()
        findLatest(task)?.let { delete(it) }
} 

There are already issues tracking giving a better error message in #297, scoping the delete in #181 and providing better options get live references inside write transactions in #298.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrong transactional state (no active transaction, wrong type of ...
I'm currently using realm for my application but when i do read n write this error shows, I have ...
Read more >
Using Transactions (The Java™ Tutorials > JDBC Database ...
This JDBC Java tutorial describes how to use JDBC API to create, insert into, update, and query tables. You will also learn how...
Read more >
Using Transactions in Quarkus
The easiest way to define your transaction boundaries is to use the @Transactional annotation on your entry method ( javax.transaction.Transactional ). @ ...
Read more >
Common Hibernate Exceptions - Baeldung
Many conditions can cause exceptions to be thrown while using Hibernate. These can be mapping errors, infrastructure problems, SQL errors, data integrity ...
Read more >
SQLSTATE values and common error codes - Db2 - IBM
25, Invalid Transaction State, Table 18 ... 40, Transaction Rollback, Table 28 ... 25000, An insert, update, or delete operation or procedure call...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found