1

i'm working on project where i have to insert and delete data from room db , so basically i was using the old approach which is to implement Asynctask for background operations but since it is no longer recommended , i decided to use Rxjava instead , i tried to implement it but i'm not getting any result so far , and this is a piece of code where it shows the insertion of data

        Completable.fromAction(new Action() {
            @SuppressLint("CheckResult")
            @Override
            public void run() throws Exception {
                recordingDb.insertRecording(modelUidd);
            }
        }).subscribeOn(Schedulers.io());

    }

And this is the deletion method

 public void DeleteData(modelUidd modelUidd) {
        Completable.fromAction(new Action() {
            @Override
            public void run() throws Exception {
                recordingDb.delete(modelUidd);
            }
        }).subscribeOn(Schedulers.io());
    }

So basically i tried to use completable with the operator fromaction , i'm not sure if what i implemented is correct or not , any help would appreciated guys , thank you

Taki
  • 3,290
  • 1
  • 16
  • 41

1 Answers1

1

The problem is that you are actually not subscribing to the observables, so nothing is happening.

To subscribe to an observable, you have to call the .subscribe() method.

I suggest that your methods defined in your DAO classes (or you "repository" classes), such as DeleteData in your example, return the Observable. Then, you can call the method in the DAO to get the Observable and subscribe to it from (ideally) a ViewModel or, if not, directly from an Activity. The moment you call the subscribe you will trigger the actual insertion or deletion, and will get a response from the onSuccess or onError defined callbacks.

For example:

public class MyViewModel extends ViewModel {

    private MyRepository myRepository;
    private final CompositeDisposable disposables;

    @Inject
    public MyViewModel(MyRepository myRepository) {
        ...
        this.myRepository = myRepository;
        disposables = new CompositeDisposable();
        ...
    }

    public void callObservableInRepository() {
         disposables.add(myRepository.myObservable()
                              .subscribe(onSuccess -> {...} , onError -> {...}));
    }

    @Override
    protected void onCleared() {
        disposables.clear();
    }

}

You can also check these two other answers for more information:

About async operations in RxJava

Using CompositeDisposable in ViewModel

dglozano
  • 6,369
  • 2
  • 19
  • 38
  • So i'm supposed to use observable instead of completable right ? and what do that @inject annotation refer to ( Dagger dependency injection ) ? thank you – Taki Mar 22 '20 at 20:54
  • So what i did initially now , i have insert method in my repository i made it return completable object , in my viewmodel , i subscribed to the completable , is that correct mate? – Taki Mar 22 '20 at 21:05
  • @takieddine When I say "Observable" I meant any base class (Completable, Single, Maybe, Observable and Flowable). I would use Completable for deletion, since it's a task that isn't returning anything, and probably Single for the insert since you might want to return the ID of the inserted entity – dglozano Mar 22 '20 at 21:12
  • @Inject annotation comes from Dagger2 dependency injection, that's correct. If you are working on a larger project, I would strongly recommend to take the time to learn it and use it. You can ignore it if you want, and just use `new` to create instance of your Repository in your ViewModel. The rest you said is correct: repository returns the RxJava base class (whatever you decide to use) and the ViewModel subscribes. You should probably also "unsubscribe" (or "dispose") from the observable in your ViewModel at some point. I unsubscribe from all of them on `onCleared()` – dglozano Mar 22 '20 at 21:15
  • it worked mate , thank you so much , so basically i had to return an object of completabled , like in this case , then i subscribed in the viewmodel and in my fragment , i call the methods directtly from viewmodel and it worked , thank you so much – Taki Mar 22 '20 at 21:24