RxJava with SQlite and ContentProvider operations

I am learning RxJava, and for this I am playing with SQLite, creating a helper class SQLiteUtilsto make it easier to handle asynchronous ContentResolver requests. For example, this is a method queryInBackground:

static
public <T> Observable<T> queryInBackground(
        final ContentResolver cr,
        final Uri uri,
        final String[] projection,
        final String selection,
        final String[] selectionArgs,
        final String sortOrder,
        final CursorHandler<T> ch) {
    return  Observable.create(new Observable.OnSubscribe<T>() {
        @Override
        public void call(Subscriber<? super T> observer) {
            if (!observer.isUnsubscribed()) {
                Cursor cursor = null;
                try {
                    cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder);
                    if (cursor != null && cursor.getCount() > 0) {
                        while (cursor.moveToNext()) {
                            observer.onNext(ch.handle(cursor));
                        }
                    }
                    observer.onCompleted();
                } catch (Exception err) {
                    observer.onError(err);

                } finally {
                    if (cursor != null) cursor.close();
                }
            }
        }
    }).subscribeOn(Schedulers.computation());
}

where CursorHandleris the interface:

/**
 * Implementations of this interface convert Cursor into other objects.
 *
 * @param <T> the target type the input Cursor will be converted to.
 */
public interface CursorHandler<T> {
    T handle(Cursor cu) throws SQLException;
}

I read the documents about Planners , but I'm not quite sure what the right choice was Schedulers.computation().

And if I would like to implement something similar to the basic operations HttpUrlConnection, with which scheduler should I choose? Schedulers.newThread()or Schedulers.io(), I would stick Schedulers.io()... but not sure.

Thanks in advance.

All the best, Luca

+4
source share
1 answer

Schedulers.io(). :

io() - , , , . , yep , .

+3

Source: https://habr.com/ru/post/1626976/


All Articles