Paging Library 3 problem when implement search with Room database

Viewed 358

I'm trying to implement a search query with Paging 3 Library. I can not find any example so currently, it looking like below. The problem is after the search query result show 2 items, but after I'm clear the filter query, first it rendered the right data (40 items), but after a timed refresh invoked by the database, it show again the searched result (2 items), then 40 items, then 2 items (repeated)

I have tried to remove the line in Repository but didn't work.

PagingRx.cachedIn(contactListPaging, viewModelScope);

My query in DAO

@Query("SELECT * FROM " +
"(SELECT * FROM contact WHERE type = 'NORMAL' AND block = 0 AND (uppercaseName LIKE :searchQuery OR tel LIKE :searchQuery) ORDER BY uppercaseName ASC)  " +
"UNION ALL SELECT * FROM " +
"(SELECT * FROM contact WHERE type != 'NORMAL' AND block = 0 AND (uppercaseName LIKE :searchQuery OR tel LIKE :searchQuery) ORDER BY uppercaseName ASC)")
public abstract PagingSource<Integer, Contact> getAvailableContactWithSearch(String searchQuery);

Repository

@Override
public Observable<PagingData<Contact>> getContactListPaging(CoroutineScope viewModelScope, @NonNull String searchQuery) {
    Observable<PagingData<Contact>> contactListPaging;
    Log.e(TAG, "getContactListPaging: searchQuery "+searchQuery +" "+searchQuery.isEmpty());

    Pager<Integer, Contact> searchContactPager = new Pager<>(
            new PagingConfig(PAGE_SIZE,
                    PREFETCH,
                    PLACEHOLDER,
                    INIT_SIZE),
            () -> mDao.getAvailableContactWithSearch("%" + searchQuery + "%"));
    contactListPaging = PagingRx.getObservable(searchContactPager);
    PagingRx.cachedIn(contactListPaging, viewModelScope);

    return contactListPaging;
}

ViewModel

@Override
public void loadContactList(String searchQuery) {
    Log.e(TAG, "loadContactList: searchQuery "+searchQuery );
    mDisposable.add(mRepository.getContactDataSource().getContactListPaging(ViewModelKt.getViewModelScope(this), searchQuery)
            .observeOn(Schedulers.io())
            .subscribeOn(Schedulers.computation())
            .subscribe(contactPagingData -> {
                contactListLiveDta.postValue(contactPagingData);
                swipeRefreshLiveData.postValue(ViewState.HIDE);
            }, error -> {
                AppLog.tagApp().e(TAG + " onError: ");
                swipeRefreshLiveData.postValue(ViewState.HIDE);
                contactListLiveDta.postValue(null);
            })
    );
}

I render it in View with

        mViewModel.getContactList().observe(getViewLifecycleOwner(), contactPagingData -> {
        Log.e(TAG, "renderListContact: adapter size "+mAdapter.getItemCount());
        if (contactPagingData != null) {
            mAdapter.submitData(getLifecycle(), contactPagingData);
        }
    });

This is the log. First I searched with "999", the result is 2 items, then I'm clear the filter, the result became 40 items.

The problem was after cleared the filter, it shows 40 items, then shows 2 items, then shows 40 items is so weird. I do not change the filter, just showing the result.

<p>2021-09-08 09:49:59.800 renderListContact: adapter size 40
2021-09-08 09:50:17.821 searchQuery 9
2021-09-08 09:50:17.958 searchQuery 99
2021-09-08 09:50:18.107 searchQuery 999
2021-09-08 09:50:18.312 loadContactList: searchQuery 999
2021-09-08 09:50:18.312 getContactListPaging: searchQuery 999 false
2021-09-08 09:50:18.325 renderListContact: adapter size 40
2021-09-08 09:50:20.712 renderListContact: adapter size 2
2021-09-08 09:50:20.719 renderListContact: adapter size 2
2021-09-08 09:50:20.722 renderListContact: adapter size 2
2021-09-08 09:50:23.752 searchQuery 99
2021-09-08 09:50:23.900 searchQuery 9
2021-09-08 09:50:24.070 searchQuery 
2021-09-08 09:50:24.273 loadContactList: searchQuery 
2021-09-08 09:50:24.273 getContactListPaging: searchQuery  true
2021-09-08 09:50:24.287 renderListContact: adapter size 2
2021-09-08 09:50:41.761 renderListContact: adapter size 40
2021-09-08 09:50:41.770 renderListContact: adapter size 40
2021-09-08 09:50:41.774 renderListContact: adapter size 40
2021-09-08 09:51:02.786 renderListContact: adapter size 40
2021-09-08 09:51:23.765 renderListContact: adapter size 2
2021-09-08 09:51:23.780 renderListContact: adapter size 2
2021-09-08 09:51:23.783 renderListContact: adapter size 2
2021-09-08 09:53:29.799 renderListContact: adapter size 2
2021-09-08 09:53:50.790 renderListContact: adapter size 40
2021-09-08 09:53:50.803 renderListContact: adapter size 40
2021-09-08 09:54:11.803 renderListContact: adapter size 40
</p>

Any help will be appreciated. Thanks in advance!

0 Answers
Related