gpt4 book ai didi

java - RxJava - 单机无法运行

转载 作者:行者123 更新时间:2023-12-01 19:57:31 25 4
gpt4 key购买 nike

我正在尝试转换此 AsyncTask:

private static class UpdateFeedTask extends AsyncTask<Void, Void, Void> {
private WeakReference<FeedFragment> weakFeedFragment;

private UpdateFeedTask(FeedFragment feedFragment) {
this.weakFeedFragment = new WeakReference<>(feedFragment);
}

@Override
protected void onPreExecute() {
if (!weakFeedFragment.get().swipeRefreshLayout.isRefreshing()) {
weakFeedFragment.get().feedProgressBar.setVisibility(View.VISIBLE);
}
}

@Override
protected final Void doInBackground(Void... voids) {
weakFeedFragment.get().submissionList = weakFeedFragment.get().paginator.next();

return null;
}

@Override
protected void onPostExecute(Void value) {
weakFeedFragment.get().feedProgressBar.setVisibility(View.INVISIBLE);
weakFeedFragment.get().swipeRefreshLayout.setRefreshing(false);
weakFeedFragment.get().isLoadingNextSubmissions = false;

FeedAdapter feedAdapter = (FeedAdapter) weakFeedFragment.get().recyclerView.getAdapter();
feedAdapter.updateItems(weakFeedFragment.get().submissionList);
}
}

进入单个Observable,但是当我调用它时,它根本不启动:

private void UpdateFeed() {
if (swipeRefreshLayout.isRefreshing()) {
feedProgressBar.setVisibility(View.VISIBLE);
}

updateFeedObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new SingleObserver<Listing<Submission>>() {

@Override public void onSubscribe(Disposable d) {
d.dispose();
}

@Override public void onSuccess(Listing<Submission> submissions) {
Log.d(TAG, "onSuccess: WORKS"); //Nope
feedProgressBar.setVisibility(View.INVISIBLE);
swipeRefreshLayout.setRefreshing(false);
isLoadingNextSubmissions = false;

FeedAdapter feedAdapter = (FeedAdapter) recyclerView.getAdapter();
feedAdapter.updateItems(submissions);
}

@Override public void onError(Throwable e) {
Log.e(TAG, e.getMessage());
}
});

}

public Single<Listing<Submission>> updateFeedObservable() {
return Single.defer(() -> Single.fromCallable(() -> {
Log.d(TAG, "updateFeedObservable: WORKS"); //Nope
return paginator.next();
}));
}

我尝试更改 Scheduler,但据我所知,任何与 IO 相关的事情都需要使用 Schedulers.io我不确定问题到底出在哪里,任何帮助将不胜感激

最佳答案

new SingleObserver<Listing<Submission>>()::onSubscribe()您立即调用d.dispose()所以你永远不会得到 onSuccess()打电话,因为当您订阅时,您会处理 SingleObserver .

删除:d.dispose()

还有这个:

return Single.defer(() -> Single.fromCallable(() -> {
Log.d(TAG, "updateFeedObservable: WORKS"); // Nope
return paginator.next();
}));

可以简化为:

return Single.fromCallable(Paginator::next); // method reference

关于java - RxJava - 单机无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087251/

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