0

I am trying to use RxJava 2 with Room ORM, but my code can not compile when I try to use RxJava 2. enter image description here

Here is the code from Contacts Dao.

@Dao
public interface ContactsDao {

@Query("SELECT * FROM contact")
Flowable<List<Contact>> getAll();

@Insert(onConflict = REPLACE)
void insert(Contact contact);

}

How I can solve this?

EDIT:

When I change to subscribeWith, I get this error: enter image description here

There is noenter image description here DisposableSubscriber.

Zookey
  • 2,637
  • 13
  • 46
  • 80

3 Answers3

2

Since RxJava2 implements the Reactive-Streams standard, subscribe returns void.

For convenience the method subscribeWith was added for the Flowable type which returns the disposable it was provided, which will make your code function as you expect.

Kiskae
  • 24,655
  • 2
  • 77
  • 74
1

You should take different overloading of the subscribe method.

  CompositeDisposable compositeDisposable = new CompositeDisposable();
  compositeDisposable.add(Observable.just(new String()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        }));

Instead of subscribing it with an Observer, try subscribing it with Consumers for onNext() and onError(). Also there is overloaded method for onComplete() with Action.

Hope it helps.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
0
    compositeDisposable.add(Flowable.just("blah").subscribeWith(new DisposableSubscriber<String>() {
                @Override
                public void onNext(String s) {

                }

                @Override
                public void onError(Throwable t) {

                }

                @Override
                public void onComplete() {

                }
            }));

This should work, so in your case it should be

disposable.add(Db.with(context).getContactsDao().findAll()
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribeWith(new DisposableSubscriber<List<Contact>>() {
                   @Override
                   public void onNext(List<Contact> contacts) {

                   }

                   @Override
                   public void onError(Throwable t) {

                   }

                   @Override
                   public void onComplete() {

                   }    
               });
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428