我正在尝试将一个RX观察者添加到我的SQLite数据库中,我肯定错过了我的实现中的一些内容,因为我的观察者的onNext()
和onCompleted()
方法都没有被调用。
以下是我的观察员:
private final Observer<List<Order>> mObjectiveObserver = new Observer<List<Order>>() {
@Override
public void onCompleted() {
System.out.println("Load completed");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(List<Order> objectives) {
System.out.println("On Next: " + objectives.size() + " elements found!");
orderAdapter.clear();
if (objectives != null) {
orderAdapter.addAll(objectives);
mCirclePulseView.setVisibility(View.INVISIBLE);
} else {
mCirclePulseView.setVisibility(View.VISIBLE);
}
orderAdapter.notifyDataSetChanged();
}
};
这些是我的LoaderCallback
方法:
@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
Loader<Cursor> loader = null;
switch (loaderId) {
case LOADER_ORDERS:
System.out.println("Create loader called");
loader = new CursorLoader(OrderManagerApplication.getAppContext(), OrderManagerContract.Order.CONTENT_URI,
QueryOrder.PROJECTION_SIMPLE, null, null, OrderManagerContract.Order.DATE_SORT);
break;
}
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (getActivity() == null) {
return;
}
if (data != null && !data.isClosed()) {
System.out.println("Finished loading orders, data not null");
switch (loader.getId()) {
case LOADER_ORDERS:
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
subscription = AndroidObservable
.bindFragment(this, DatabaseHelper.mainOrdersObservable(data))
.subscribeOn(Schedulers.computation())
.unsubscribeOn(AndroidSchedulers.mainThread())
.subscribe(mObjectiveObserver);
System.out.println("I should be here, onLoadFinished");
break;
}
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
内容观察员代码如下:
class HomeOrdersContentObserver extends ContentObserver {
private int mLoaderId = 0;
public HomeOrdersContentObserver(Handler handler, int loaderId) {
super(handler);
mLoaderId = loaderId;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if (getActivity() == null) {
return;
}
Bundle bundle = new Bundle();
//bundle.putString(FILTER_TXT, lastFilterQ);
restartLoader(mLoaderId, bundle);
}
}
public void restartLoader(int loaderId, Bundle args) {
getLoaderManager().restartLoader(loaderId, args, this);
}
我已将日志放在我的代码中的任何地方,除了这些onNext
和onCompleted
方法之外,它们应该按原样打印。我的实施中可能缺少什么想法?
答案 0 :(得分:3)
我发现上述代码存在一些问题。
1st:你实际上并没有在任何地方调用onNext / onCompleted。既然你正在尝试连接这两个范例(Loader&amp; Rx),那么你需要在onLoadFinished中放入onNext(如果你想在没有数据的情况下调用它,则会出现onError,但这将关闭订阅)和onCompleted在onLoaderReset
第二:你正在重做onLoadFinished中的订阅,我认为你不想要这个 - 你为什么要在每次有新数据时重新订阅?您应该在创建加载器时执行此操作,并在销毁加载器时取消订阅(onLoaderReset)。
这是一种可能的实施方式:
@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
Loader<Cursor> loader = null;
switch (loaderId) {
case LOADER_ORDERS:
System.out.println("Create loader called");
loader = new CursorLoader(OrderManagerApplication.getAppContext(), OrderManagerContract.Order.CONTENT_URI,
QueryOrder.PROJECTION_SIMPLE, null, null, OrderManagerContract.Order.DATE_SORT);
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
subscription = AndroidObservable
.bindFragment(this, DatabaseHelper.mainOrdersObservable(data))
.subscribeOn(Schedulers.computation())
.unsubscribeOn(AndroidSchedulers.mainThread())
.subscribe(mObjectiveObserver);
break;
}
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (getActivity() == null) {
return;
}
if (data != null && !data.isClosed()) {
System.out.println("Finished loading orders, data not null");
switch (loader.getId()) {
case LOADER_ORDERS:
mObjectiveObserver.onNext(data);
System.out.println("I should be here, onLoadFinished");
break;
}
} else {
mObjectiveObserver.onError("No data available");
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}
请记住,这样结构化,如果没有数据,它实际上会关闭订阅(onError会这样做),因此您将无法从加载器接收更多数据。如果您不想这样,那么在上面的onError的情况下,您实际上会使用null或new ArrayList调用onNext,然后在您的观察者中处理它。