Question

I am trying to add an RX observer to my SQLite database and I am surely missing something from my implementation as neither the onNext() and onCompleted() methods from my observer are not getting called.

Here is my observer:

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();
        }
    };

These are my LoaderCallback methods:

@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) {

    }

The content observer code follows below:

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);
    }

I have put logs everywhere in my code and they get printed as they should, except for this onNext and onCompleted methods. Any ideas what I might be missing from my implementation?

Was it helpful?

Solution

I see a few problems with the above code.

1st: you're not actually calling onNext/onCompleted anywhere. Since you're trying to connect the two paradigms (Loader & Rx), then you would need to put onNext in the onLoadFinished (with onError in case you want it called when there's no data, but that will close the subscription) and onCompleted in onLoaderReset

2nd: you're redoing the subscription in onLoadFinished, which I don't think you'd want - why would you want to resubscribe every time you have new data? You should do it when creating the loader, and unsubscribe when destroying the loader (onLoaderReset).

This is one possible implementation:

@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();
        }
    }

Have in mind that structured like this, in case of no data it will actually close the subscription (onError does that) so you won't be able to receive any more data from the loader. If you don't want that, then in case of onError above, you would actually call onNext with null or new ArrayList and then take care of it in your observer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top