I can see two solutions to this, either use a looper thread (HandlerThread) with setAutoRefresh(true)
(and call setAutoRefresh(false)
before Looper.quit()
), or force a refresh for the Realm instance on the thread.
NOTE: This relies on package-internal methods. Beware.
In v 1.1.1 (and v1.2.0), – and any version before 3.0.0 – instead of the following line
// mRealm.waitForChange(); / mRealm.refresh();
You could force the update on the local thread through the HandlerController
associated with the Realm instance using package-internal stuff
package io.realm;
public class RealmRefresh {
public static void refreshRealm(Realm realm) {
Message message = Message.obtain();
message.what = HandlerControllerConstants.LOCAL_COMMIT;
realm.handlerController.handleMessage(message);
}
}
And then call
mRealm = Realm.getDefaultInstance();
RealmHelper.saveObj(myRealmObject, mRealm);
RealmRefresh.refreshRealm(mRealm);
mRealm.close();
Please note the change log’s breaking changes though, because 0.89.0 changed iteration behavior, and results are no longer live during an active transaction; but from 3.0.0 they are again.
However, I must also note that if your NotificationListenerService
is running in a remote process, then the Realm instances won’t be able to notify each other.
EDIT:
In Realm Java 3.0.0, the notification behavior was changed completely, and HandlerController
no longer exists.
Instead, the following should work:
package io.realm;
public class RealmRefresh {
public static void refreshRealm(Realm realm) {
realm.sharedRealm.refresh();
}
}
EDIT:
In Realm 3.2.+, this is all available with
realm.refresh();