onCreateLoader和onLoadFinished不同步

时间:2014-08-12 16:48:46

标签: java android android-loadermanager

我正在使用LoaderManager.LoaderCallbacks加载我的数据,问题是data正确提取的onCreateLoader未委托给onLoadFinished。确切地说,onLoadFinished已运行,但我无法获取myData提取的onCreateLoader数据

public Loader> onCreateLoader(int id,Bundle args){

    return new ThrowableLoader<List<ParseUser>>(getActivity(), users) {

        @Override
        public List<ParseUser> loadData() throws Exception {
            try {
                if(getActivity() != null) {
                    ParseQuery<ParseUser> query = ParseUser.getQuery();
                    query.orderByAscending(Constants.ParseConstants.KEY_USERNAME);
                    query.setLimit(1000);

                    query.findInBackground(new FindCallback<ParseUser>() {
                        public void done(List<ParseUser> objects, ParseException e) {
                                myData = objects;

                            } else {
                                System.out.
                                        println("Fetch Users failed" + e);
                            }
                        }
                    });
                } else {
                    return Collections.emptyList();
                }

            } catch (Exception e) {
                Activity activity = getActivity();
                if (activity != null)
                    activity.finish();
                return null;
            }
            return null;
        }
    };
}

以下是ThrowableLoader

的实施
public abstract class ThrowableLoader<D> extends AsyncLoader<D> {


    private final D data;

    private Exception exception;

    /**
     * Create loader for context and seeded with initial data
     *
     * @param context
     * @param data
     */
    public ThrowableLoader(Context context, D data) {
        super(context);

        this.data = data;
    }

    @Override
    public D loadInBackground() {
        exception = null;
        try {
            return loadData();
        } catch (Exception e) {
            Ln.d(e, "Exception loading data");
            exception = e;
            return data;
        }
    }

    /**
     * @return exception
     */
    public Exception getException() {
        return exception;
    }

    /**
     * Clear the stored exception and return it
     *
     * @return exception
     */
    public Exception clearException() {
        final Exception throwable = exception;
        exception = null;
        return throwable;
    }

    /**
     * Load data
     *
     * @return data
     * @throws Exception
     */
    public abstract D loadData() throws Exception;
}

public abstract class AsyncLoader<D> extends AsyncTaskLoader<D> {
    private D data;

    /**
     * Create async loader
     *
     * @param context
     */
    public AsyncLoader(Context context) {
        super(context);
    }

    @Override
    public void deliverResult(D data) {
        if (isReset())
            // An async query came in while the loader is stopped
            return;

        this.data = data;

        super.deliverResult(data);
    }

    @Override
    protected void onStartLoading() {
        if (data != null)
            deliverResult(data);

        if (takeContentChanged() || data == null)
            forceLoad();
    }

    @Override
    protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        cancelLoad();
    }

    @Override
    protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();

        data = null;
    }
}

1 个答案:

答案 0 :(得分:1)

您的方法存在的问题是query.findInBackground()是一个异步调用,它将立即返回。根据设计,您放在Loader's loadInBackground()中的代码必须是同步的。您可以切换到使用普通ParseQuery.find(),因为loadInBackground()已经在工作线程上运行。

相关问题