Viktor
Viktor

Reputation: 561

Realm handling two transactions on background thread

I have a large set of data, that I need to do some optimilization upon. The background thread looks like this:

Realm realm;
try{
 realm = Realm.getDefaultInstance();
 realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
           //delete some data from table A according to algorithm
        }
    });
} finally {
        if (realm != null) {
            realm.close();
        }
}

While at the same time the user can delete the whole table A, if he decides to, before the optimization finishes, which starts another background thread, just like the one above deleting the whole table A.

I would like to know what happens, if one transaction gets finished before the other, do I need to ensure that only one of these transaction happens at a given time?

Thanks in advance....

Upvotes: 1

Views: 1366

Answers (3)

Godfather
Godfather

Reputation: 848

This is default behavior. Only one transaction will happen at a time .When you are in a transaction it's blocking and no other thread can perform other transaction.

Basically, you can't start multiple transaction while you are already in a transaction. It will throw an Exception Check this Link .

Upvotes: 2

SamyB
SamyB

Reputation: 235

Try to use executeTransactionAsync instead of executeTransaction, the process will be treated in a background thread. Don't be worry about the transaction end, realm manage that for us

Upvotes: 1

EpicPandaForce
EpicPandaForce

Reputation: 81588

The first transaction will cause the second transaction to be blocked until the first transaction is either committed or cancelled.

Write transactions are blocking across threads and processes.

Upvotes: 2

Related Questions