Android 带有SQlite和ContentProvider操作的RxJava
我正在学习RxJava,为此我正在使用SQLite,编写一个帮助类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
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;
}