Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Realm executeTransactionAsync close the instance

Tags:

android

realm

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?

like image 470
Psest328 Avatar asked Jan 04 '23 04:01

Psest328


1 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();
        }
    });
}
like image 100
EpicPandaForce Avatar answered Jan 26 '23 23:01

EpicPandaForce