Search code examples
androidrx-java2greendao

RxJava2 Observable Issue


I am new to RXJava2, so need some help, I have to implement the functionality of favourite, when user clicks on fav then it's inserted in the greendo database and when he unfavourite the same event then it's removed from the greendo database.

I am able to insert and fetch the result but not getting the idea how to remove it.

This line in the below code returning me void mDaoSession.getFavouriteDao().deleteByKey(favourite.getId());

It's saying incompatible type then I how to make observable compatible with void return type.

@Override
    public Observable<Long> deleteFavouriteEvent(Favourite favourite) {
        return Observable.fromCallable(new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                return mDaoSession.getFavouriteDao().deleteByKey(favourite.getId());
            }
        });
    }

Insertion working fine :

 @Override
    public Observable<Long> insertFavouriteEvent(Favourite favourite) {
        return Observable.fromCallable(new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                return mDaoSession.getFavouriteDao().insert(favourite);
            }
        });
    }

Solution

  • You can use Completable.fromAction instead of Observable.

    public Completable deleteFavouriteEvent(Favourite favourite) {
        return Completable.fromAction(new Action() {
            @Override
            public void run() throws Exception {
                mDaoSession.getFavouriteDao().deleteByKey(favourite.getId());;
            }
        });
    }