有关从后台线程数据的变化适当通知AsyncTaskLoader的 [英] Proper notification of AsyncTaskLoader about data changes from background thread

查看:137
本文介绍了有关从后台线程数据的变化适当通知AsyncTaskLoader的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施 AsyncTaskLoader 我的自定义数据源:

 公共类数据源{
    公共接口DataSourceObserver {
        无效onDataChanged();
    }
    ...
}
 

数据源将保持登记的观察员的名单,并通知他们的变化。 CustomLoader 将实施 DataSourceObserver 。现在的问题是如何正确地通知 CustomLoader ,因为 Loader.onContentChanged()必须从UI线程,但在我的情况被称为数据源操作(和调用 DataSourceObserver.onDataChanged())会从后台线程完成。

更新了观念,从Selvin提示

 公共类CustomLoader扩展AsyncTaskLoader< ...>实现DataSource.DataSourceObserver {
    私人最终处理程序observerHandler;

    公共CustomLoader(上下文的背景下){
        超(上下文);
        observerHandler =新的处理程序()
    }

    @覆盖
    公共无效onDataChanged(){
        observerHandler.post(新的Runnable(){
            @覆盖
            公共无效的run(){
                onContentChanged();
            }
        });
    }
}
 

解决方案

我已经在一个情况下,这是非常类似于你使用本地广播了很多的成功。该方法涉及到一个 AsyncTaskLoader 的实施,将注册一个的BroadcastReceiver 监听,描述发生了什么改变一个特定的字符串。这的BroadcastReceiver 有一个参考的装载机键,通话 onContentChanged 。当数据需要更新,使本地广播与上述字符串和的BroadcastReceiver 会听到它,引发的负载。下面是一些例子code,如果你删除它可能无法很好地工作,我概括了一些类名,但希望你会得到的想法:

广播接收器在装载机Implmentation使用:

 公共类LoaderBroadcastReceiver扩展的BroadcastReceiver
{
    私人装载机装载机;

    公共LoaderBroadcastReceiver(装载机装载机)
    {
        this.loader =装载机;
    }

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图)
    {
        loader.onContentChanged();
    }
}
 

装载机实施注册接收器在 onStartLoading()

 私人LoaderBroadcastReceiver loaderBroadcastReceiver = NULL;

@覆盖
保护无效onStartLoading()
{
     // ...一些code在这里

    如果(loaderBroadcastReceiver == NULL)
    {
        loaderBroadcastReceiver =新LoaderBroadcastReceiver(本);
        LocalBroadcastManager.getInstance(的getContext())registerReceiver(loaderBroadcastReceiver,新的IntentFilter(NEWDATASTRING))。
    }

    // ...更多code在这里
}
 

最后,在这里是如何的 onDataChanged在数据源将使广播。这将需要一个上下文来帮助发送广播。因为这可以从任意一个线程调用,我会用你的ApplicationContext,因为从活动的背景下可能会导致问题,如果活动被销毁。

 公共类的DataSource
{
    公共接口DataSourceObserver
    {
        无效onDataChanged(上下文的applicationContext)
        {
            LocalBroadcastManager.getInstance(上下文).sendBroadcast(新的意向书(NEWDATASTRING));
        }
    }
    ...
}
 

您可能会想用它玩了一下,看看它是如何为你工作。你可以用不同的字符串来区分,需要加载不同的数据。您还需要注销接收器在某个时候,也许就在 onReset()。让我知道如果任何这在不清楚的意见,我会尽我所能,以澄清。

I want to implement AsyncTaskLoader for my custom data source:

public class DataSource {
    public interface DataSourceObserver {
        void onDataChanged();
    }
    ...
}

DataSource will keep list of registered observers and will notify them about changes. CustomLoader will implement DataSourceObserver. The question is how to properly notify CustomLoader since Loader.onContentChanged() must be called from UI thread but in my case DataSource operations (and calls to DataSourceObserver.onDataChanged()) will be done from background threads.

Updated with idea from 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 a lot of success using Local Broadcasts in a case that's very similar to yours. The method involves an AsyncTaskLoader implementation that will register a BroadcastReceiver listening for a particular String that describes what's changed. This BroadcastReceiver keeps a reference to the Loader and calls onContentChanged. When the data needs a refresh, make the Local Broadcast with the aforementioned String and the BroadcastReceiver will hear it and trigger the load. Here's some example code, it may not work perfectly if you drop it in, I've generalized some class names, but hopefully you'll get the idea:

Broadcast Receiver to be used in your 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 the Receiver in onStartLoading()

private LoaderBroadcastReceiver loaderBroadcastReceiver = null;

@Override
protected void onStartLoading()
{
     //... some code here

    if(loaderBroadcastReceiver == null)
    {
        loaderBroadcastReceiver = new LoaderBroadcastReceiver(this);
        LocalBroadcastManager.getInstance(getContext()).registerReceiver(loaderBroadcastReceiver, new IntentFilter("NEWDATASTRING"));
    }

    //... some more code here
}

Finally, here's how onDataChanged in DataSource will make the Broadcast. It'll need a Context to help send the Broadcast. Since this can be called from an arbitrary Thread, I'd use your ApplicationContext, since an Context from an Activity could cause problems if the Activity is destroyed.

public class DataSource 
{
    public interface DataSourceObserver 
    {
        void onDataChanged(Context applicationContext)
        {
            LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("NEWDATASTRING"));
        }
    }
    ...
}

You'll probably want to play with it a bit to see how it works for you. You can use different Strings to differentiate different data that needs loading. You'll also want to unregister the Receiver at some point, perhaps in onReset(). Let me know if any of this in unclear in the comments, I'll try my best to clarify.

这篇关于有关从后台线程数据的变化适当通知AsyncTaskLoader的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆