Source:
public void run() { if (checkAndReportInvalidState()) { return; } Realm.Transaction transaction = new Realm.Transaction() { @Override public void execute(Realm realm) { if (checkAndReportInvalidState()) { return; } realm.insertOrUpdate(unmanagedResponse); } }; Realm.Transaction.OnSuccess onSuccess = new Realm.Transaction.OnSuccess() { @Override public void onSuccess() { if (checkAndReportInvalidState()) { return; } managedResponse = managementRealm.where(PermissionOfferResponse.class).equalTo("id", responseId).findFirstAsync(); RealmObject.addChangeListener(managedResponse, new RealmChangeListener<PermissionOfferResponse>() { @Override public void onChange(final PermissionOfferResponse response) { if (checkAndReportInvalidState()) { RealmObject.removeChangeListener(managedResponse, this); return; } handleServerStatusChanges(response, new Runnable() { @Override public void run() { RealmObject.removeAllChangeListeners(managedResponse); grantedPermissionResults = permissionRealm.where(Permission.class).equalTo("path", response.getPath()).findAllAsync(); grantedPermissionResults.addChangeListener(new RealmChangeListener<RealmResults<Permission>>() { @Override public void onChange(RealmResults<Permission> permissions) { if (!permissions.isEmpty()) { grantedPermissionResults.removeChangeListener(this); notifyCallbackWithSuccess(managedResponse.getRealmUrl(), permissions.first()); } } }); } }); } }); } }; Realm.Transaction.OnError onError = new Realm.Transaction.OnError() { @Override public void onError(Throwable error) { if (checkAndReportInvalidState()) { return; } notifyCallbackError(new ObjectServerError(ErrorCode.UNKNOWN, error)); } }; transactionTask = managementRealm.executeTransactionAsync(transaction, onSuccess, onError); }

Target:
public void run() { if (checkAndReportInvalidState()) { return; } Realm.Transaction transaction = new Realm.Transaction() { @Override public void execute(Realm realm) { if (checkAndReportInvalidState()) { return; } realm.insertOrUpdate(unmanagedResponse); } }; Realm.Transaction.OnSuccess onSuccess = new Realm.Transaction.OnSuccess() { @Override public void onSuccess() { if (checkAndReportInvalidState()) { return; } managedResponse = managementRealm.where(PermissionOfferResponse.class).equalTo("id", responseId).findFirstAsync(); RealmObject.addChangeListener(managedResponse, new RealmChangeListener<PermissionOfferResponse>() { @Override public void onChange(final PermissionOfferResponse response) { if (checkAndReportInvalidState()) { RealmObject.removeChangeListener(managedResponse, this); return; } handleServerStatusChanges(response, new Runnable() { @Override public void run() { grantedPermissionResults = permissionRealm.where(Permission.class).equalTo("path", response.getPath()).findAllAsync(); grantedPermissionResults.addChangeListener(new RealmChangeListener<RealmResults<Permission>>() { @Override public void onChange(RealmResults<Permission> permissions) { if (!permissions.isEmpty()) { grantedPermissionResults.removeChangeListener(this); notifyCallbackWithSuccess(managedResponse.getRealmUrl(), permissions.first()); } } }); } }); } }); } }; Realm.Transaction.OnError onError = new Realm.Transaction.OnError() { @Override public void onError(Throwable error) { if (checkAndReportInvalidState()) { return; } notifyCallbackError(new ObjectServerError(ErrorCode.UNKNOWN, error)); } }; transactionTask = managementRealm.executeTransactionAsync(transaction, onSuccess, onError); }