使用 sqlite 开始使用 RxJava

Getting started with RxJava with sqlite

我正在学习 RxJava 并浏览了几篇文章和视频。我对 RxJava 可以提供的东西感到非常棒,所以我认为目前正在了解响应式编程的全部内容。 This tutorial by Dan Lew 在一定程度上帮助我理解了使用 RxJava 的基础知识。但是我发现,我越是自以为完全理解了 RxJava,我脑海中冒出的问题就越多。

由于我是一个在编写代码时学到最多的人,所以这就是我正在尝试做的事情。我已经有一个使用 SQLite 的应用程序。我的应用程序有 7 个饼图,它们从数据库游标获取数据。每个饼图从光标处获取不同行的数据(浮点值),并根据其数据生成图表。

我现在要做的是,我想使用 RxJava 从数据库中检索数据并填充图表。但是我不知道 RxJava 应该如何与数据库交互。我是否需要使用任何其他库来执行此操作?因为我发现 rxjava-jdbc and SqlBrite which I think should help me with this, but do I really need them? I also found a 似乎没有使用任何额外的库(当然除了 RxJava)而且他似乎使用 ContentObservable 但我仍然没有完全理解它。此外,回答他问题的人甚至不使用 ContentObservable。而且我只知道 Android 有自己的 Observable class.. 与 RxJava 一起使用是否完全兼容?如果不是,为什么同名?

有人请帮助我...

============================================= ===========

回应以下 GreyBeardedGeek 的解决方案:

这是我从 sqlite 检索数据的 Callable

public class GetIncome implements Callable<Map<String,String>> {
    public static final String FILE_NAME = "CGetIncome";

    Context myContext;
    int year, month;

    public GetIncome(Context context, int getYear, int getMonth){
        myContext = context;
        year = getYear;
        month = getMonth;
    }

    @Override
    public Map<String,String> call() throws Exception {
        Map<String,String> output = new HashMap<>();
        JarControl jc = new JarControl(myContext);
        JSONObject getIncome = jc.getIncome(year,month,0,0);

        output.put("necessities",getIncome.getString("necessities"));
        output.put("savings",getIncome.getString("savings"));

        return output;
    }
}

下面是我尝试使用主 activity 中的 Callable 的方式:

Callable<Map<String,String>> getIncome = new GetIncome(getContext(),2015,9);

现在我不知道如何将可调用的 getIncome 放入 Observables 中。你提到 rx 中的 fromCallables 应该这样做..但实际上该怎么做?据this tutorial that shows how to use Observable.from, so I expect to be able to use Callables by doing Observable.fromCallable but apparently the function doesn't exist. Quick googled showed that .fromCallable should be used by doing something like Async.fromCallable(), but since I'm very new to RxJava, I can only understand how to create callable according to how the tutorial显示...

很抱歉我对 RxJava 的极度菜鸟

我敢肯定会有很多其他意见,但到目前为止,我已经采取了几种不同的方法:

  • 如果你想要相对少量的数据(并且你可以保证它总是有一个已知的大小上限),那么:

    • 创建访问数据库的同步方法,获取游标,迭代游标以创建包含数据的数据结构(例如列表),然后return存储它。
    • 创建调用方法的 Callable
    • 使用rx.fromCallable创建一个Observable
  • 如果您需要无限行数,请执行与上述类似的操作,但 return 游标。

编辑:示例代码:

private Map<String, String> getDataFromDatabase() { 
   Map<String, String> result = new HashMap<>(); 
   // do whatever you need to (i.e. db query, cursor) to fill it in 
   return result; 
} 

private Callable<Map<String, String>> getData() { 
   return new Callable() { 
      public Map<String, String> call() { 
        return getDataFromDatabase(); 
    } 
}

// in utility class  RxUtil.java 
public static <T> Observable<T> makeObservable(final Callable<T> func) {
        return Observable.create(
                new Observable.OnSubscribe<T>() {
                    @Override
                    public void call(Subscriber<? super T> subscriber) {
                        try {
                            T observed = func.call();
                            if (observed != null) { // to make defaultIfEmpty work
                                subscriber.onNext(observed);
                            }
                            subscriber.onCompleted();
                        } catch (Exception ex) {
                            subscriber.onError(ex);
                        }
                    }
                });
    }


// and finally, 
public Observable<Map<String, String>> getDataObservable() {
   return RxUtil.makeObservable(getData());
}