简体   繁体   English

Jetpack Paging3 库对同一页面进行多次调用

[英]Jetpack Paging3 library makes several calls to the same page

I am trying to implement Jetpack paging 3 library following thecodelab using room database as source of truth and a RemoteMediator.我正在尝试按照使用房间数据库作为真实来源和 RemoteMediator 的代码实验室来实现 Jetpack paging 3 库。 The app queries the google books api but for some reason when I perform a search it makes several calls to the same page.该应用程序查询谷歌图书 API,但出于某种原因,当我执行搜索时,它会多次调用同一页面。 For example I get this in the log when I search fire without scrolling:例如,当我在不滚动的情况下搜索火时,我在日志中得到了这个:

D/BooksRepository: new search: fire
D/BooksRemoteMediator: title: fire, page: 0
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0 (648ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 1
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1 (608ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 0
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0 (629ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 1
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1 (843ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 0
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=0 (527ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 1
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=1 (734ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 2
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=2
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=2 (783ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 3
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=3
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=3 (769ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 2
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=2
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=2 (521ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 3
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=3
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=3 (549ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 2
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=2
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=2 (966ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 3
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=3
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=3 (673ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 4
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=4
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=4 (634ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 5
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=5
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=5 (604ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 4
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=4
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=4 (632ms, unknown-length body)
D/BooksRemoteMediator: title: fire, page: 3
I/okhttp.OkHttpClient: --> GET https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=3
I/okhttp.OkHttpClient: <-- 200 https://www.googleapis.com/books/v1/volumes?q=intitle%3Afire&key=AIzaSyBxHmT9nFCp9n2uOHkS3Gcq2OO3zbxaMrw&maxResults=40&startIndex=3 (602ms, unknown-length body)

My implementation is like this:我的实现是这样的:

Repository:存储库:

class BooksRepository(private val service: BookService, private val database: BooksDatabase) {
    companion object {
        private const val NETWORK_PAGE_SIZE = 40
    }

    fun getSearchResultStream(
        title: String = "",
        author: String = "",
        publisher: String = "",
        isbn: String = ""
    ): Flow<PagingData<Book>> {
        Timber.d("new search: $title")
        val dbQuery = "%${title.replace(' ', '%')}%"
        val pagingSourceFactory =  { database.bookDao.getBooks(dbQuery, author, publisher)}

        return Pager(
            config = PagingConfig(
                pageSize = NETWORK_PAGE_SIZE,
                enablePlaceholders = false
            ),
            remoteMediator = BooksRemoteMediator(title, author, publisher, isbn, apiKey, service, database),
            pagingSourceFactory = pagingSourceFactory
        ).flow
    }
}

RemoteMediator:远程中介:

private const val BOOKS_STARTING_PAGE_INDEX = 0
@OptIn(ExperimentalPagingApi::class)
class BooksRemoteMediator(
    private val title: String?,
    private val author: String?,
    private val publisher: String?,
    private val isbn: String?,
    private val key: String,
    private val service: BookService,
    private val booksDatabase: BooksDatabase
) : RemoteMediator<Int, Book>() {

    override suspend fun load(loadType: LoadType, state: PagingState<Int, Book>): MediatorResult {
        val page = when (loadType) {
            LoadType.REFRESH -> {
                val remoteKeys = getRemoteKeyClosestToCurrentPosition(state)
                remoteKeys?.nextKey?.minus(1) ?: BOOKS_STARTING_PAGE_INDEX
            }
            LoadType.PREPEND -> {
                val remoteKeys = getRemoteKeyForFirstItem(state)
                if (remoteKeys == null) {
                    // The LoadType is PREPEND so some data was loaded before,
                    // so we should have been able to get remote keys
                    // If the remoteKeys are null, then we're an invalid state and we have a bug
                    throw InvalidObjectException("Remote key and the prevKey should not be null")
                }
                // If the previous key is null, then we can't request more data
                val prevKey = remoteKeys.prevKey
                if (prevKey == null) {
                    return MediatorResult.Success(endOfPaginationReached = true)
                }
                remoteKeys.prevKey
            }
            LoadType.APPEND -> {
                val remoteKeys = getRemoteKeyForLastItem(state)
                if (remoteKeys == null || remoteKeys.nextKey == null) {
                    throw InvalidObjectException("Remote key should not be null for $loadType")
                }
                remoteKeys.nextKey
            }
        }

        Timber.d("title: $title, page: $page")
        val sb = StringBuilder()
        if (!title.isNullOrBlank()) sb.append("$TITLE$title+")
        if (!author.isNullOrBlank()) sb.append("$AUTHOR$author+")
        if (!publisher.isNullOrBlank()) sb.append("$PUBLISHER$publisher+")
        if (!isbn.isNullOrBlank()) sb.append("$ISBN$isbn+")
        sb.setLength(sb.length - 1)
        val apiQuery = sb.toString()

        try {
            val apiResponse = service.searchBooks(apiQuery, key, state.config.pageSize, page)

            val books = apiResponse.items
            val endOfPaginationReached = books.isEmpty()
            booksDatabase.withTransaction {
                // clear all tables in the database
                if (loadType == LoadType.REFRESH) {
                    booksDatabase.remoteKeysDao.clearRemoteKeys()
                    booksDatabase.bookDao.clearBooks()
                }
                val prevKey = if (page == BOOKS_STARTING_PAGE_INDEX) null else page - 1
                val nextKey = if (endOfPaginationReached) null else page + 1
                val keys = books.map {
                    RemoteKeys(bookId = it.id, prevKey = prevKey, nextKey = nextKey)
                }
                booksDatabase.remoteKeysDao.insertAll(keys)
                booksDatabase.bookDao.insert(books)
            }
            return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)
        } catch (exception: IOException) {
            return MediatorResult.Error(exception)
        } catch (exception: HttpException) {
            return MediatorResult.Error(exception)
        }
    }

    private suspend fun getRemoteKeyForLastItem(state: PagingState<Int, Book>): RemoteKeys? {
        // Get the last page that was retrieved, that contained items.
        // From that last page, get the last item
        return state.pages.lastOrNull() { it.data.isNotEmpty() }?.data?.lastOrNull()
            ?.let { book ->
                // Get the remote keys of the last item retrieved
                booksDatabase.remoteKeysDao.remoteKeysBookId(book.id)
            }
    }

    private suspend fun getRemoteKeyForFirstItem(state: PagingState<Int, Book>): RemoteKeys? {
        // Get the first page that was retrieved, that contained items.
        // From that first page, get the first item
        return state.pages.firstOrNull { it.data.isNotEmpty() }?.data?.firstOrNull()
            ?.let { book ->
                // Get the remote keys of the first items retrieved
                booksDatabase.remoteKeysDao.remoteKeysBookId(book.id)
            }
    }

    private suspend fun getRemoteKeyClosestToCurrentPosition(
        state: PagingState<Int, Book>
    ): RemoteKeys? {
        // The paging library is trying to load data after the anchor position
        // Get the item closest to the anchor position
        return state.anchorPosition?.let { position ->
            state.closestItemToPosition(position)?.id?.let { bookId ->
                booksDatabase.remoteKeysDao.remoteKeysBookId(bookId)
            }
        }
    }

}

ViewModel:视图模型:

class BookListViewModel(private val repository: BooksRepository) : ViewModel() {
    private var currentQueryValue: String? = null

    private var currentSearchResult: Flow<PagingData<Book>>? = null

    fun searchRepo(queryString: String): Flow<PagingData<Book>> {
        val lastResult = currentSearchResult
        if (queryString == currentQueryValue && lastResult != null) {
            return lastResult
        }
        currentQueryValue = queryString
        val newResult: Flow<PagingData<Book>> = repository.getSearchResultStream(queryString)
            .cachedIn(viewModelScope)
        currentSearchResult = newResult
        return newResult
    }
}

Fragment:分段:

class BookListFragment : Fragment() {
...

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        setHasOptionsMenu(true)
        binding = FragmentBookListBinding.inflate(inflater, container, false).apply {
            lifecycleOwner = viewLifecycleOwner
            viewModel = bookListViewModel
        }
        initAdapter()

        val queryString = queryArgs.split(",")
        val query = savedInstanceState?.getString(LAST_SEARCH_QUERY) ?: DEFAULT_QUERY
        search(query)

        setHasOptionsMenu(true)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        binding.retryButton.setOnClickListener { adapter.retry() }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString(LAST_SEARCH_QUERY, latest)
    }

    private fun search(query: String) {
        // Make sure we cancel the previous job before creating a new one
        searchJob?.cancel()
        searchJob = lifecycleScope.launch {
            bookListViewModel.searchRepo(query).collectLatest{
                adapter.submitData(it)
            }
        }
    }

    private fun initAdapter() {
        binding.rvBooks.adapter = adapter.withLoadStateHeaderAndFooter(
            header = BooksLoadStateAdapter { adapter.retry() },
            footer = BooksLoadStateAdapter { adapter.retry() }
        )

        adapter.addLoadStateListener { loadState ->
            // Only show the list if refresh succeeds.
            binding.rvBooks.isVisible = loadState.source.refresh is LoadState.NotLoading
            // Show loading spinner during initial load or refresh.
            binding.pbLoading.isVisible = loadState.source.refresh is LoadState.Loading
            // Show the retry state if initial load or refresh fails.
            binding.retryButton.isVisible = loadState.source.refresh is LoadState.Error

            // Toast on any error, regardless of whether it came from RemoteMediator or PagingSource
            val errorState = loadState.source.append as? LoadState.Error
                ?: loadState.source.prepend as? LoadState.Error
                ?: loadState.append as? LoadState.Error
                ?: loadState.prepend as? LoadState.Error
            errorState?.let {
                Toast.makeText(
                    context,
                    "\uD83D\uDE28 Wooops ${it.error}",
                    Toast.LENGTH_LONG
                ).show()
            }
        }
    }

    private fun initSearch(menu: Menu) {
        val searchItem = menu.findItem(R.id.action_search)
        val searchView = searchItem.actionView as android.widget.SearchView
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener, android.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                try {
                    latest = query!!
                    updateBookListFromInput(query)
                } catch (e: Exception) {
                    Timber.d(e)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })
        lifecycleScope.launch {
            adapter.loadStateFlow
                // Only emit when REFRESH LoadState changes.
                .distinctUntilChangedBy { it.refresh }
                // Only react to cases where REFRESH completes i.e., NotLoading.
                .filter { it.refresh is LoadState.NotLoading }
                .collect { binding.rvBooks.scrollToPosition(0) }
        }
    }

    private fun updateBookListFromInput(query: String?) {
        query?.trim().let {
            if (!it.isNullOrEmpty()) {
                search(it.toString())
            }
        }
    }


    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.book_list_menu, menu)
        initSearch(menu)
        _menu = menu
        val recentList: ArrayList<String> = SpUtil.getQueryList(requireContext())
        var recentMenu: MenuItem? = null
        for (item in recentList) {
            recentMenu = menu.add(Menu.NONE, recentList.indexOf(item), Menu.NONE, item)
        }
    }

    companion object {
        private const val LAST_SEARCH_QUERY: String = "last_search_query"
        private const val DEFAULT_QUERY = "Fishing"
    }
}

Dao:道:

@Dao
interface BooksDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(books: List<Book>)

    @Query("DELETE FROM books")
    suspend fun clearBooks()

    @Query("SELECT * FROM books WHERE (title LIKE :title) OR (authors LIKE :author) " +
            "OR (publisher LIKE :publisher) ORDER BY title ASC")
    fun getBooks(title: String, author: String = "", publisher: String = ""): PagingSource<Int, Book>
}

Service:服务:

interface BookService {
    @GET("volumes")
    suspend fun searchBooks(
        @Query("q") query: String,
        @Query("key") apiKey: String,
        @Query("maxResults") max: Int,
        @Query("startIndex") page: Int
    ): BookSearchResponse
}

It would be great if someone point out what I am doing wrong and help fix this problem.如果有人指出我做错了什么并帮助解决这个问题,那就太好了。 Thank you谢谢

I am getting exactly the same error on another API.我在另一个 API 上遇到了完全相同的错误。 The version that doesn't use Remote Mediator works fine.不使用远程调解器的版本工作正常。 When I add Remote Mediator it Refreshes, Appends, Then gets on a Prepend loop.当我添加远程调解器时,它会刷新、附加,然后进入 Prepend 循环。

What I have tried so far:到目前为止我尝试过的:

  1. Match the codelabs gradle dependency versions;匹配 codelabs gradle 依赖版本;
  2. Placing logs on the viewmodel to see if I was making the request several times.在视图模型上放置日志以查看我是否多次发出请求。
  3. Placing logs on the RemoteMediator class to get the previous, current and next pages and the LoadType that is currently happeningRemoteMediator类上放置日志以获取上一页、当前页和下一页以及当前发生的LoadType

This is what happens without scrolling:这是不滚动时发生的情况:

    2020-07-26 17:43:56.859 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = REFRESH
    2020-07-26 17:43:58.279 327-385/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:43:58.503 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 2 and LoadType = APPEND
    2020-07-26 17:44:00.185 327-375/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: 1 Next Key: 3
    2020-07-26 17:44:00.303 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:00.575 327-375/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:00.601 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 2 and LoadType = APPEND
    2020-07-26 17:44:00.776 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:01.056 327-385/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:01.185 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:01.336 327-386/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:01.630 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:01.830 327-385/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:01.913 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:02.444 327-387/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:02.567 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:02.716 327-385/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:02.810 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:02.983 327-387/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:03.070 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:03.239 327-385/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:03.348 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:03.521 327-387/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:03.665 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:04.102 327-385/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:04.232 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:04.476 327-386/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:04.577 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:05.857 327-387/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2
    2020-07-26 17:44:06.570 327-327/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator: Current Page: 1 and LoadType = PREPEND
    2020-07-26 17:44:06.932 327-386/com.doiliomatsinhe.mymovies D/MoviesRemoteMediator$load: Prev Key: null Next Key: 2

This is how my RemoteMediator Load is:这是我的 RemoteMediator 负载的方式:

override suspend fun load(loadType: LoadType, state: PagingState<Int, DatabaseMovie>): MediatorResult {
    val page = when (loadType) {
        LoadType.REFRESH -> {
            val remoteKeys = getRemoteKeyClosestToCurrentPosition(state)
            Timber.d("REFRESH-> ${remoteKeys?.prevKey} -> ${remoteKeys?.nextKey} ")
            remoteKeys?.nextKey?.minus(1) ?: MOVIES_LIST_STARTING_PAGE
        }
        LoadType.PREPEND -> {
            val remoteKeys = getRemoteKeyForFirstItem(state)
            if (remoteKeys == null) {
                throw InvalidObjectException("Remote key and the prevKey should not be null")
            }
            // If the previous key is null, then we can't request more data
            if (remoteKeys.prevKey == null) {
                return MediatorResult.Success(endOfPaginationReached = true)
            }
            Timber.d("PREPEND-> ${remoteKeys.prevKey} -> ${remoteKeys.nextKey} ")
            remoteKeys.prevKey

        }

        LoadType.APPEND -> {
            val remoteKeys = getRemoteKeyForLastItem(state)
            if (remoteKeys?.nextKey == null) {
                throw InvalidObjectException("Remote key should not be null for $loadType")
            }
            Timber.d("APPEND-> ${remoteKeys.prevKey} -> ${remoteKeys.nextKey} ")
            remoteKeys.nextKey
        }
    }

    Timber.d("Page= $page, LoadType= $loadType")
    return try {
        val response = if (category != null && language != null) {
            service.getMovies(category, SECRET_KEY, language, page) } else {
            throw InvalidObjectException("Query Parameters can't be NULL")
        }
        val movies = response.results
        val endOfPaginationReached = movies.isEmpty()

        database.withTransaction {
            if (loadType == LoadType.REFRESH) {
                database.moviesDao.clearMovies()
                database.remoteKeysDao.clearRemoteKeys()
            }

            val prevKey = if (page == MOVIES_LIST_STARTING_PAGE) null else page - 1
            val nextKey = if (endOfPaginationReached) null else page + 1
            Timber.d("PrevKey =$prevKey, NextKey=$nextKey")
            val keys = movies.map {
                RemoteKeys(
                    movieId = it.id,
                    prevKey = prevKey,
                    nextKey = nextKey
                )
            }
            database.moviesDao.insertAllMovies(*movies.asDatabaseModel())
            database.remoteKeysDao.insertAll(keys)

        }

        MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)
    } catch (exception: IOException) {
        MediatorResult.Error(exception)
    } catch (exception: HttpException) {
        MediatorResult.Error(exception)
    }
}

数据库中的primarykey如果来自网络请求可能会导致这个问题

The issue might be with incorrect primary key in your Books table.问题可能在于 Books 表中的主键不正确。 Can you share what is your你能分享一下你的吗

I spent some time with this problem and after reading user9694585 and dunkypie answers, I was able to solve my problem.我花了一些时间解决这个问题,在阅读了 user9694585 和 dunkypie 的答案后,我解决了我的问题。 In my case, I was using the movieDb API, exactly as Doilio Matsinhe, and I discover that I can't rely on the API id for two reasons:就我而言,我使用的是 movieDb API,与 Doilio Matsinhe 完全一样,我发现我不能依赖 API id 有两个原因:

  • The remote ids were out of order on the same page.同一页面上的远程 ID 乱序。 So if I save them on the table as is, I lose the order from the server.因此,如果我将它们按原样保存在桌子上,则会丢失服务器的订单。
  • If the ids are out of order I couldn't reach the real first/last items of the page.如果 id 出现问题,我将无法到达页面的真正第一个/最后一个项目。 I think this part that makes the mediator be in a loop.我认为这部分使中介陷入循环。

So what I ended up doing was making the remote id as an attribute and creating a primary key with autoGenerate = true on movie table:所以我最终做的是将远程 ID 作为一个属性,并在电影表上创建一个带有autoGenerate = true的主键:

@Entity(tableName = "movie")    
data class MovieTable(
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0,
    var remoteId: Long = 0,
    ...
)
@Dao
interface RemoteKeysDao {
   ...
    @Query("SELECT * FROM remote_keys WHERE movieId = :movieId")
    suspend fun remoteKeyId(movieId: Long): RemoteKeyTable?
   ...
}

My RemoteMediator is very similar to Doilio Matsinhe.我的 RemoteMediator 与 Doilio Matsinhe 非常相似。 So I leave only the methods that were not in his answer:所以我只留下他的回答中没有的方法:

private suspend fun getRemoteKeyForLastItem(state: PagingState<Int, MovieTable>): RemoteKeyTable? {
    return state.pages
        .lastOrNull {
            it.data.isNotEmpty()
        }?.data?.lastOrNull()
        ?.let { movie ->
            remoteKeyLocalDataSource.remoteKeyId(movie.remoteId)
        }
}

private suspend fun getRemoteKeyForFirstItem(state: PagingState<Int, MovieTable>): RemoteKeyTable? {
    return state.pages
        .firstOrNull {
            it.data.isNotEmpty()
        }?.data?.firstOrNull()
        ?.let { movie ->
            remoteKeyLocalDataSource.remoteKeyId(movie.remoteId)
        }
}

private suspend fun getRemoteKeyClosestToCurrentPosition(
    state: PagingState<Int, MovieTable>
): RemoteKeyTable? {
    return state.anchorPosition?.let { position ->
        state.closestItemToPosition(position)?.remoteId?.let { id ->
            remoteKeyLocalDataSource.remoteKeyId(id)
        }
    }
}

Basically is the one that we have on the codelab but using the movie.remoteId to get the remoteKey.id.基本上是我们在 codelab 上使用的那个,但是使用 movie.remoteId 来获取 remoteKey.id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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