android - Proper notification of AsyncTaskLoader about data changes from background thread -


i want implement asynctaskloader custom data source:

public class datasource {     public interface datasourceobserver {         void ondatachanged();     }     ... } 

datasource keep list of registered observers , notify them changes. customloader implement datasourceobserver. question how notify customloader since loader.oncontentchanged() must called ui thread in case datasource operations (and calls datasourceobserver.ondatachanged()) done background threads.

updated idea selvin tip:

public class customloader extends asynctaskloader<...> implements datasource.datasourceobserver {     private final handler observerhandler;      public customloader(context context) {         super(context);         observerhandler = new handler()     }      @override     public void ondatachanged() {         observerhandler.post(new runnable() {             @override             public void run() {                 oncontentchanged();             }         });     } } 

i've had lot of success using local broadcasts in case that's similar yours. method involves asynctaskloader implementation register broadcastreceiver listening particular string describes what's changed. broadcastreceiver keeps reference loader , calls oncontentchanged. when data needs refresh, make local broadcast aforementioned string , broadcastreceiver hear , trigger load. here's example code, may not work if drop in, i've generalized class names, you'll idea:

broadcast receiver used in loader implmentation:

public class loaderbroadcastreceiver extends broadcastreceiver {     private loader loader;      public loaderbroadcastreceiver(loader loader)     {         this.loader = loader;     }      @override     public void onreceive(context context, intent intent)     {         loader.oncontentchanged();     }  } 

loader implementation registers receiver in onstartloading()

private loaderbroadcastreceiver loaderbroadcastreceiver = null;  @override protected void onstartloading() {      //... code here      if(loaderbroadcastreceiver == null)     {         loaderbroadcastreceiver = new loaderbroadcastreceiver(this);         localbroadcastmanager.getinstance(getcontext()).registerreceiver(loaderbroadcastreceiver, new intentfilter("newdatastring"));     }      //... more code here } 

finally, here's how ondatachanged in datasource make broadcast. it'll need context send broadcast. since can called arbitrary thread, i'd use applicationcontext, since context activity cause problems if activity destroyed.

public class datasource  {     public interface datasourceobserver      {         void ondatachanged(context applicationcontext)         {             localbroadcastmanager.getinstance(context).sendbroadcast(new intent("newdatastring"));         }     }     ... } 

you'll want play bit see how works you. can use different strings differentiate different data needs loading. you'll want unregister receiver @ point, perhaps in onreset(). let me know if of in unclear in comments, i'll try best clarify.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -