如何使用Android Room和RxJava 2没有结果?

Zoo*_*key 14 android rx-java2 android-room

我有数据库与表联系,我想检查是否有联系某些电话号码.

@Query("SELECT * FROM contact WHERE phone_number = :number")
Flowable<Contact> findByPhoneNumber(int number);
Run Code Online (Sandbox Code Playgroud)

我有RxJava 2 Composite一次性声明,以检查是否有电话号码联系.

disposable.add(Db.with(context).getContactsDao().findByPhoneNumber(phoneNumber)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new DisposableSubscriber<Contact>() {
                @Override
                public void onNext(final Contact contact) {
                    Log.d("TAG", "phone number fined");
                    Conversation conversation;
                    if(contact != null){
                        conversation = Db.with(context).getConversationsDao().findBySender(contact.getContactId());
                        if(conversation != null){
                            conversation.setUpdatedAt(Utils.getDateAndTimeNow());
                            saveConversation(contact, conversation, context, text, phoneNumber, false);
                        } else {
                            conversation = getConversation(contact, contact.getPhoneNumber());
                            saveConversation(contact, conversation, context, text, phoneNumber, true);
                        }
                    } else {
                        conversation = Db.with(context).getConversationsDao().findByPhone(phoneNumber);
                        if(conversation != null){
                            conversation.setUpdatedAt(Utils.getDateAndTimeNow());
                            saveConversation(contact, conversation, context, text, phoneNumber, false);
                        } else {
                            conversation = getConversation(contact, phoneNumber);
                            saveConversation(contact, conversation, context, text, phoneNumber, true);
                        }
                    }
                }

                @Override
                public void onError(Throwable t) {
                    Log.d("TAG", "find phone number throwable");
                    Toast.makeText(context, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                }

                @Override
                public void onComplete() {
                    Log.d("TAG", "onComplete");
                }
            }));
Run Code Online (Sandbox Code Playgroud)

如果查询可以找到所需电话号码的联系人,这工作正常,但如果有结果,则没有任何反应.

以下是我编写的两个测试用例,它们运行正常:

@RunWith(AndroidJUnit4.class)
public class ContactsTest {

    private AppDatabase db;

    @Rule
    public InstantTaskExecutorRule instantTaskExecutorRule =
            new InstantTaskExecutorRule();

    @Before
    public void initDb() throws Exception {
        db = Room.inMemoryDatabaseBuilder(
                InstrumentationRegistry.getContext(),
                AppDatabase.class)
                // allowing main thread queries, just for testing
                .allowMainThreadQueries()
                .build();
    }

    @After
    public void close(){
        db.close();
    }

    @Test
    public void insertAndFindTest(){
        final Contact contact = new Contact();
        contact.setName("Test");
        contact.setPhoneNumber(555);
        db.contactsDao()
                .insert(contact);

        db.contactsDao().findByPhoneNumber(contact.getPhoneNumber())
                .test()
                .assertValue(new Predicate<Contact>() {
                    @Override
                    public boolean test(@NonNull Contact savedContact) throws Exception {
                        if(savedContact.getPhoneNumber() == contact.getPhoneNumber()){
                            return true;
                        }
                        return false;
                    }
                });
    }

    @Test
    public void findNoValues(){
        db.contactsDao().findByPhoneNumber(333)
                .test()
                .assertNoValues();
    }

}
Run Code Online (Sandbox Code Playgroud)

我怎么能解决这个问题?

小智 26

至于说在这里,你可以使用MaybeSingle用于这种情况下:

也许

@Query("SELECT * FROM Users WHERE id = :userId")
Maybe<User> getUserById(String userId);
Run Code Online (Sandbox Code Playgroud)

这是发生的事情:

  • 当数据库中没有用户且查询没有返回任何行时,Maybe将完成.
  • 当数据库中有用户时,Maybe将触发onSuccess并且它将完成.
  • 如果用户在完成可能之后更新,则不会发生任何事情.

@Query("SELECT * FROM Users WHERE id = :userId")
Single<User> getUserById(String userId);
Run Code Online (Sandbox Code Playgroud)

以下是一些场景:

  • 当数据库中没有用户并且查询没有返回任何行时,Single将触发onError(EmptyResultSetException.class)
  • 当数据库中有用户时,Single将触发onSuccess.
  • 如果在调用Single.onComplete之后更新了用户,则没有任何操作,因为流已完成.

它是在1.0.0-alpha5版本中添加的.

  • 重要提示:“如果 Single&lt;T&gt; 包含集合的类型参数(例如 Single&lt;List&lt;Song&gt;&gt;),则不会抛出此异常(`EmptyResultSetException.class`)**,并发出空集合反而。” [文档](https://developer.android.com/reference/androidx/room/EmptyResultSetException.html) (2认同)

Wik*_*ski 12

如果您只想使用您的实体一次,Single或者Maybe就足够了.但是如果你想观察你的查询是否更新了你可以使用Flowable并包装你的对象List,所以当没有结果你会得到空列表,然后在更新数据库后,你将获得另一个事件列表中的结果.

@Query("SELECT * FROM contact WHERE phone_number = :number LIMIT 1")
Flowable<List<Contact>> findByPhoneNumber(int number)
Run Code Online (Sandbox Code Playgroud)

我相信它在某些情况下很有用.缺点是你必须访问像对象一样resultList.get(0)


pel*_*lus 5

当您在类中使用Flowable(并且LiveData也)作为返回值时,您Dao的查询永远不会停止发送数据,因为 Room 正在监视表中的数据更改。引用官方文档:

此外,如果响应是可观察的数据类型,例如 Flowable 或 LiveData,Room 会监视查询中引用的所有表是否失效。

不确定处理这种情况的最佳方法是什么,但对我有用的是一个很好的老.timeout()操作员。请查看以下测试并关注评论:

@Test
public void shouldCompleteIfForced() throws InterruptedException {
    // given
    TestScheduler testScheduler = new TestScheduler();

    // when asking db for non existent project
    TestSubscriber<Project> test = projectDao.getProject("non existent project")
            .timeout(4, TimeUnit.SECONDS, testScheduler)
            .test();

    // then hang forever waiting for first emission which will never happen
    // as there is no such project
    test.assertNoValues();
    test.assertNotComplete();
    test.assertNoErrors();

    // when time passes and we trigger timeout() operator
    testScheduler.advanceTimeBy(10, TimeUnit.SECONDS);

    // then finally break stream with TimeoutException error ...
    test.assertError(TimeoutException.class);
}
Run Code Online (Sandbox Code Playgroud)