Close Realm instance after completion

I want to close the Realm instance in executeTransactionAsyncafter completion. The reason is that the main thread of my applications continues to freeze, I think the reason for this is because the background real instance does not close after execution is complete. See my code below:

realm.executeTransactionAsync(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        // Execute realm code
                        realm.copyToRealmOrUpdate(myData);
                        // Can I close the realm instance here without getting an 
                        // error? realm.close(); causes an error.
                    }
                }, new Realm.Transaction.OnSuccess() {
                    @Override
                    public void onSuccess() {
                        Log.i("CB", "success");
                        // looks like I cannot access the execute Realm 
                        // instance here.
                        // Closing realm.getDefaultInstance does not change my issue
                    }
                }, new Realm.Transaction.OnError() {
                    @Override
                    public void onError(Throwable error) {
                        Log.i("CB", "error - " + error.getMessage());
                    }
                });
            }

Please view my comments. My application screen just goes black. The execution succeeds and onSuccess()is called, but I cannot access the scope instance executeto close it here.

Do you have any suggestions as to what I can try? Am I doing something wrong?

Thanks in advance.

EDIT

07-19 11:43:42.379 8146-8146/com.shortterminsurance.shortterm I/CB: success
07-19 11:43:43.258 8146-8152/com.shortterminsurance.shortterm W/art: Suspending all threads took: 33.234ms
07-19 11:43:43.266 8146-8156/com.shortterminsurance.shortterm I/art: Background partial concurrent mark sweep GC freed 476307(17MB) AllocSpace objects, 512(10MB) LOS objects, 40% free, 33MB/55MB, paused 7.261ms total 163.497ms
07-19 11:43:44.131 8146-8156/com.shortterminsurance.shortterm I/art: Background sticky concurrent mark sweep GC freed 408160(9MB) AllocSpace objects, 459(15MB) LOS objects, 35% free, 35MB/55MB, paused 10.287ms total 147.823ms
07-19 11:43:44.834 8146-8152/com.shortterminsurance.shortterm W/art: Suspending all threads took: 103.676ms
07-19 11:43:44.848 8146-8156/com.shortterminsurance.shortterm W/art: Suspending all threads took: 13.424ms

onSuccess. , execute - : (.

+4
4

,

@Override
public void execute(Realm realm) {

. , .

Realm.executeTransactionAsync(),

bgRealm.beginTransaction();
try {
    transaction.execute(bgRealm);

    if (!Thread.currentThread().isInterrupted()) {
        bgRealm.commitTransaction(false, new Runnable() {
            @Override
            public void run() {
                // The bgRealm needs to be closed before post event to caller handler to avoid
                // concurrency problem. eg.: User wants to delete Realm in the callbacks.
                // This will close Realm before sending REALM_CHANGED.
                bgRealm.close();
            }
        });
        transactionCommitted = true;
    }
}
...

, .execute() , , .

+10

:

            final Realm realm = Realm.getDefaultInstance();
            realm.executeTransactionAsync(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    // Execute realm code
                    realm.copyToRealmOrUpdate(myData);
                }
            }, new Realm.Transaction.OnSuccess() {
                @Override
                public void onSuccess() {
                    Log.i("CB", "success");
                    realm.close();
                }
            }, new Realm.Transaction.OnError() {
                @Override
                public void onError(Throwable error) {
                    Log.i("CB", "error - " + error.getMessage());
                    realm.close();
                }
            });
        }

.

+3

Tim Castelijns, executeTransactionAsync , , , , , Realm,

+1
source
final Realm realmFirstInstance = Realm.getDefaultInstance();
realmFirstInstance.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                // Execute realm code
                realm.copyToRealmOrUpdate(myData);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                Log.i("CB", "success");
                realmFirstInstance.close();
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Log.i("CB", "error - " + error.getMessage());
                realmFirstInstance.close();
            }
        });
    }

Also, for a clearer understanding: remember to close the first instance of the area. Because here "realmFirstInstance" and "realm" inside the execute () method are two different objects.

0
source

Source: https://habr.com/ru/post/1648351/


All Articles