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

公共加载程序> 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;
    }
}

最佳答案

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

08-05 03:49