realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Support listening to changes on Non-looper threads

Open cmelchior opened this issue 11 years ago • 3 comments

All our change listeners depend on the UI thread to function. This means it can be tricky to tell background threads when to update the realm instances to the latest version. Also right now we don't warn people if they register an listener on a non-looper thread.

This means there are a number of choices we can make

a) Throw exception when using listener on non-looper threads. ? b) Allow listeners and find some other way of triggering them?

Would a pattern like the below make sense? It would effectively let people create their own event queue just for Realm events:

Realm realm = Realm.getDefaultInstance();
// Blocking method that doesn't return until Realm is changed and refreshed
// It would also automatically call all listeners
while(realm.waitForChange()) {  // return true if changed, return false if aborted
  // Work
}
realm.close();

cmelchior avatar Nov 09 '15 10:11 cmelchior

Just out of curiosity. What would be Realm's standpoint when it comes to an area like this?

If it is to follow Android way (if there ever is such a thing), I'd imagine the above case should just throw an exception saying 'there isn't a legitimate looper so put one on'. It might be very itching but for those ones who are accustomed to Android way would get a very consistent message and eventually appreciate it. I mean, you can basically expect what's going to happen.

On the other hand, if an automatic situational detector kicks in and provides a pattern like above, it would be very soothing, and deliver a contrast message, 'we've got your back'.

I am not saying which one is right or wrong. I'm just only small thing like that could be a good indicator to show the idea. (If my comment is totally irrelevant, you can erase.)

stk1m1 avatar Dec 23 '15 15:12 stk1m1

+1

falmanna avatar Aug 18 '16 17:08 falmanna

We added Realm.waitForChange() and Realm.stopWaitForChange() a long time ago, which should solve this use case.

cmelchior avatar Aug 20 '20 18:08 cmelchior