Realm Execute Transaction on Android – Java Example

Android Realm

This Java example code shows how to run or execute a write transaction in Realm database on Android using executeTransaction() or executeTransactionAsync() methods.

If the code in the callback throws an exception, Realm cancels the transaction and rollback. Otherwise, Realm commits the transaction.

— Unlike read operations, write operations in Realm must be wrapped in transactions.
— At the end of a write operation, you can either commit the transaction or cancel it.

Transaction Block

Instead of manually keeping track of beginTransaction, commitTransaction, and cancelTransaction, you can use the executeTransaction method, which will automatically handle begin, commit, and cancel if an error happens.

Asynchronous Transaction

Since transactions are blocked by other transactions, you might want to write on a background thread to avoid blocking the UI thread. By using an asynchronous transaction, Realm will run that transaction on a background thread.

Share This Post On: