简体   繁体   中英

Code can not compile when I try to use RxJava 2 with Room

I am trying to use RxJava 2 with Room ORM, but my code can not compile when I try to use RxJava 2. 在此处输入图片说明

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: 在此处输入图片说明

There is no 在此处输入图片说明 DisposableSubscriber.

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.

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 Consumer s for onNext() and onError() . Also there is overloaded method for onComplete() with Action .

Hope it helps.

    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() {

                   }    
               });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM