android - RealmChangeListener does not get called when Realm gets updated in NotificationListenerService -


i'm doing realm insertions on extended notificationlistenerservice, this:

public class nlservice extends notificationlistenerservice {      @override     public void onnotificationposted(statusbarnotification sbn) {          // building realmobject here          mrealm = realm.getdefaultinstance();         realmhelper.saveobj(myrealmobject, mrealm);     //  mrealm.waitforchange(); / mrealm.refresh();         mrealm.close();     } }   public class realmhelper {      public static realmmodel saveobj(realmobject realmobject, realm realm) {         realm.begintransaction();         realmobject newrealmobject = realm.copytorealm(realmobject);         realm.committransaction();         return newrealmobject;     } } 

using realm newer v0.88.3, not single realmchangelistener (rcl) gets called if gets inserted in nlservice.

i tried attaching rcl's directly realm, realmresults , realmobject, nothing works.

the app has example simple rcl's realmresults<myrealmobject>.size() , several recycleradapters , rcl inside realmrecyclerviewadapter never called.

rerunning queries works , "missing data" shows up. if gets inserted on ui- or other thread, rcl's called , "missing data" shows up.

i stayed months on realm 0.88.3 because can not bring work newer realm version. 0.88.3 mrealm.refresh(); called in nlservice, not available in newer versions , .waitforchange blocks endlessly.

manifest.xml:

<service     android:name=".service.nlservice"     android:label="@string/nl_service"     android:permission="android.permission.bind_notification_listener_service">     <intent-filter>         <action android:name="android.service.notification.notificationlistenerservice"/>     </intent-filter> </service> 

i can see 2 solutions this, either use looper thread (handlerthread) setautorefresh(true) (and call setautorefresh(false) before looper.quit()), or force refresh realm instance on thread.


note: relies on package-internal methods. beware.

in v 1.1.1 (and v1.2.0), - , version before 3.0.0 - instead of following line

//  mrealm.waitforchange(); / mrealm.refresh(); 

you force update on local thread through handlercontroller associated 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 call

    mrealm = realm.getdefaultinstance();     realmhelper.saveobj(myrealmobject, mrealm);     realmrefresh.refreshrealm(mrealm);     mrealm.close(); 

please note change log's breaking changes though, because 0.89.0 changed iteration behavior, , results no longer live during active transaction; 3.0.0 again.


however, must note if notificationlistenerservice running in remote process, realm instances won't able notify each other.



edit:

in realm java 3.0.0, notification behavior changed completely, , handlercontroller no longer exists.

instead, following should work:

package io.realm;  public class realmrefresh {     public static void refreshrealm(realm realm) {         realm.sharedrealm.refresh();     } } 

edit:

in realm 3.2.+, available with

realm.refresh(); 

Comments