入门RxJava使用SQLite [英] Getting started with RxJava with sqlite

查看:639
本文介绍了入门RxJava使用SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习RxJava的进程,并直通了几篇文章和视频。我完全感觉真棒什么RxJava可以提供,所以目前我觉得得到了什么反​​应式编程是所有关于感。 本教程由丹·卢帮助我有点明白使用RxJava的基本知识。但我发现,我越觉得我完全理解RxJava,更多的问题来飞出我的脑海里。

I'm in the process of learning RxJava and have went thru several articles and videos. I totally felt awesome of what RxJava can offer, so I think currently getting the sense of what Reactive Programming is all about. This tutorial by Dan Lew helped me somewhat to understand the basics of using RxJava. But I found out that the more I thought I've fully understood RxJava, the more questions come popping out of my mind.

由于我一个人写code时,谁最获悉,这里就是我想要做的。我已经有一个使用SQLite的应用程序。我的应用程序有7个饼图,其中从数据库获取光标它的数据。每个饼图得到一个不同的行从光标数据(浮点值),并生成图表,根据它的数据。

Since I'm a guy who learns most when writing code, here's what I'm trying to do. I already have an app that utilized SQLite. My app has 7 pie chart, which get its data from a Database cursor. Each of the pie chart gets a different row of data (float values) from the cursor, and generates the chart according to its data.

我试图做现在的问题是,我想用RxJava检索数据库中的数据并填充图。但是,我在我的损失就RxJava应该如何与数据库进行交互。我是否需要使用任何额外的库来做到这一点?因为我发现 rxjava-JDBC 和的 SqlBrite 我认为应该帮助我,但我真的需要它们?我还发现了一个<一个href=\"http://stackoverflow.com/questions/30075663/flatten-observableobservablecursor-to-observablecursor\">question在SO 这似乎不使用任何附加的库(除当然RxJava),他似乎使用ContentObservable,但我还是不完全明白这一点。除了谁​​回答了他的问题,甚至不使用ContentObservable的家伙。我只知道,Android有它自己的观测类..它是完全兼容,能够与RxJava一起使用吗?如果不是,为什么相同的名称?

What I'm trying to do now is, I want to use RxJava to retrieve the data from the database and populate the chart. But I'm at loss on how RxJava should interact with database. Do I need to use any additional library to do this? Because I found rxjava-jdbc and SqlBrite which I think should help me with this, but do I really need them? I also found a question in SO which doesn't seem to use any additional library (except for RxJava of course) and he seems to use ContentObservable but I still don't fully get it. Besides the guy who answered his question doesn't even use ContentObservable. And I just knew that Android has its own Observable class.. is it fully compatible to be used together with RxJava? If not, why the same name?.

有人请帮我...

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

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

在响应低于GreyBeardedGeek的解决方案:

In response to GreyBeardedGeek's solution below:

这是我的赎回,从sqlite的检索数据

This is my Callable that retrieves data from sqlite

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

和这里就是我想要使用的可调用从我的主要活动:

And here's how I'm trying to use the Callable from my main activity:

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

现在我不知道如何把getIncome赎回成观测量。你提到的RX应该这样做fromCallables ..但实际上该怎么办呢?据本教程演示如何使用Observable.from ,所以我希望能够做Observable.fromCallable但显然函数不存在使用可调用。快速谷歌搜索显示,.fromCallable应通过执行类似Async.fromCallable()可以使用,但因为我是很新的RxJava,我只能理解如何根据怎样的教程显示...

Now I don't know how to put the getIncome callable into Observables. You mentioned fromCallables in rx should do that.. but actually how to do it? According to 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 shows...

和我在RxJava极端noobness

And sorry for my extreme noobness on RxJava

推荐答案

我敢肯定,有将是一堆其他的意见,但到目前为止,我已经采取了几个不同的方法:

I'm sure that there are going to be a bunch of other opinions, but so far, I've taken a couple of different approaches:


  • 如果你想要的数据相对少量的(你可以保证它永远有大小已知上限),则:

  • If you want a relatively small amount of data (and you can guarantee that it will always have a known upper bound on size), then:


  • 创建访问数据库,获得一个光标的同步方法,迭代光标来创建一个数据结构包含的数据(例如一个名单),然后返回。

  • 创建一个可调用的调用方法

  • 使用rx.fromCallable创建一个可观察

如果你需要行的无限数量的,做类似上面的东西,但返回的Cursor。

If you need an unbounded number of rows, do something similar to the above, but return the Cursor.

编辑:样品code:

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

这篇关于入门RxJava使用SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆