gpt4 book ai didi

java - onCreateLoader 和 onLoadFinished 不同步

转载 作者:行者123 更新时间:2023-12-01 12:39:17 27 4
gpt4 key购买 nike

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

公共(public)加载器> 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 loadInBackground() 中的代码必须是同步的。您可以切换到使用普通的 ParseQuery.find(),因为 loadInBackground() 已经在工作线程上运行。

关于java - onCreateLoader 和 onLoadFinished 不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25269794/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com