Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有SQlite和ContentProvider操作的RxJava_Android_Sqlite_Rx Java - Fatal编程技术网

Android 带有SQlite和ContentProvider操作的RxJava

Android 带有SQlite和ContentProvider操作的RxJava,android,sqlite,rx-java,Android,Sqlite,Rx Java,我正在学习RxJava,为此我正在使用SQLite,编写一个帮助类SQLiteUtils,以帮助更轻松地处理异步ContentResolver查询。 例如,这是queryInBackground方法: static public <T> Observable<T> queryInBackground( final ContentResolver cr, final Uri uri, final String[] projec

我正在学习RxJava,为此我正在使用SQLite,编写一个帮助类
SQLiteUtils
,以帮助更轻松地处理异步ContentResolver查询。 例如,这是
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());
}
我已经阅读了有关的文档,但我不太确定
Schedulers.computation()
是否是正确的选择

如果我想为基本
HttpUrlConnection
操作实现类似的东西,我应该选择哪个调度器
Schedulers.newThread()
Schedulers.io()
,我会坚持使用
Schedulers.io()
…但不确定

提前谢谢

祝你一切顺利, luca

根据您的需要,您应该使用
Schedulers.io()
。相关报价:

io()由一个无限的线程池支持,是一种类似的东西 你会使用非计算密集型的任务,这就是 不会给CPU带来太多负载。所以,是的,与文件交互 系统中,与不同主机上的数据库或服务的交互 好例子


非常感谢您的有用链接@卢卡斯,我很乐意。你也可能想看看SQLBrite
/**
 * 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;
}