Does Realm executeTransactionAsync close the instance

Viewed 2835

The title says it all. I've done some searching but haven't found anything concrete.

Do I need to call realm.close after doing realm.executeTransactionAsync or does the async transaction handle that?

Thank you

EDIT: Per EpidPandaForce, executeTransactionAsync closes the background realm instance when complete.

But what is the proper way to close the realm instance if executeTransactionAsync is called from the UI thread? In the transactions onSuccess/onFailure?

2 Answers

You seem like you're looking for the following scenario.

public void doWrite(MyObject obj) {
    Realm realm = Realm.getDefaultInstance(); 
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            bgRealm.insert(obj); // assuming obj is unmanaged
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            realm.close();
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            realm.close();
        }
    });
}

In Kotlin

fun doWrite(obj: RealmObject) {
        val realm = Realm.getDefaultInstance()
        realm.executeTransactionAsync({ bgRealm ->
            bgRealm.insert(obj) // assuming obj is unmanaged
        }, { realm.close() }, { realm.close() })
    }
Related