如何在RxJava Observable中更新数据库?

时间:2018-07-27 00:18:20

标签: java android multithreading rx-java observable

我正在用Android / Java运行一个程序来模拟运动队的赛季,因为我有Team对象。我有一个Model类,用于通过ContentResolver / ContentProvider与数据库进行通信。

我正在使用RxJava与Schedulers.io线程上的数据库进行通信,以免阻塞主线程。

在我的“ InsertTeam”方法中,我可以很好地插入数据库中。但是,在我的“ UpdateTeam”方法中,未运行可观察项/订阅,并且我的数据库未得到更新(即,调用了该方法,但未运行可观察项-没有任何反应)。
但是,如果我在AndroidSchedulers.mainThread()上运行可观察到的UpdateTeam,则它可以工作(但会降低程序速度)!

这对我来说没有任何意义。有谁知道为什么我的可观察更新可以在MainThread上运行而不能在Schedulers.io线程上运行?

InsertTeamMethod

 @Override
public void insertTeam(final Team team) {

    //Insert a team into the database

    Observable<Uri> insertTeamObservable = Observable.fromCallable(new Callable<Uri>() {
        @Override
        public Uri call() throws Exception {
            String name = team.getName();
            double elo = team.getELO();
            double offRating = team.getOffRating();
            double defRating = team.getDefRating();
            int currentWins = team.getWins();
            int currentLosses = team.getLosses();
            int currentDraws = team.getDraws();
            int division = team.getDivision();

            ContentValues values = new ContentValues();
            values.put(TeamEntry.COLUMN_TEAM_NAME, name);
            values.put(TeamEntry.COLUMN_TEAM_ELO, elo);
            values.put(TeamEntry.COLUMN_TEAM_OFF_RATING, offRating);
            values.put(TeamEntry.COLUMN_TEAM_DEF_RATING, defRating);
            values.put(TeamEntry.COLUMN_TEAM_CURRENT_WINS, currentWins);
            values.put(TeamEntry.COLUMN_TEAM_CURRENT_LOSSES, currentLosses);
            values.put(TeamEntry.COLUMN_TEAM_CURRENT_DRAWS, currentDraws);
            values.put(TeamEntry.COLUMN_TEAM_DIVISION, division);

            //Insert values into database
            Uri uri = contentResolver.insert(TeamEntry.CONTENT_URI, values);
            return uri;
        }
    });

    insertTeamObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Uri>() {
        @Override
        public void onSubscribe(Disposable d) {
            mCompositeDisposable.add(d);
        }

        @Override
        public void onNext(Uri uri) {
            team.setUri(uri);
        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    });


}

UpdateTeamMethod

@Override
public void updateTeam(final Team team) {

    //Update a team in the database

    Log.d("Before team obs",  "" + team.getName());

    Observable<Integer> updateTeamObservable = Observable.fromCallable(new Callable<Integer>() {
        @Override
        public Integer call() throws Exception {
            //Update team database wins, losses and win loss pct values
            ContentValues values = new ContentValues();
            values.put(TeamEntry.COLUMN_TEAM_CURRENT_WINS, team.getWins());
            values.put(TeamEntry.COLUMN_TEAM_CURRENT_LOSSES, team.getLosses());
            values.put(TeamEntry.COLUMN_TEAM_CURRENT_DRAWS, team.getDraws());
            values.put(TeamEntry.COLUMN_TEAM_WIN_LOSS_PCT, team.getWinLossPct());

            int rowsUpdated = contentResolver.update(team.getUri(), values, null, null);

            Log.d("Team updated:  ", "" + team.getName());

            return rowsUpdated;
        }
    });

    updateTeamObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Integer>() {
        @Override
        public void onSubscribe(Disposable d) {
            mCompositeDisposable.add(d);
        }

        @Override
        public void onNext(Integer rowsUpdated) {
            Log.d("Team rows updated: ", " " + rowsUpdated);
        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    });


}

0 个答案:

没有答案