title: Android Paging 3 and RemoteMediator requests for list while user doesn't reached to bottom of the list
body: <p>I was working with Android Paging 3 as you can see in <a href="https://developer.android.com/topic/libraries/architecture/paging/v3-network-db" rel="nofollow noreferrer">https://developer.android.com/topic/libraries/architecture/paging/v3-network-db</a>.
  When I run the app <strong>RemoteMediator</strong> requests server to get List until there is nothing on the server.</p>
  <p>The point is here that we want to get the list when user reached the bottom of the list, but RemoteMediator is requesting until get all of list on the server!</p>
  <p>RemoteMediator.kt</p>
  <pre><code>    @OptIn(ExperimentalPagingApi::class)
  class ExampleRemoteMediator(
    private val query: String,
    private val database: RoomDb,
    private val networkService: ExampleBackendService
  ) : RemoteMediator&lt;Int, User&gt;() {
    val userDao = database.userDao()
    val remoteKeyDao = database.remoteKeyDao()
  
    override suspend fun load(
      loadType: LoadType,
      state: PagingState&lt;Int, User&gt;
    ): MediatorResult {
      return try {
        // The network load method takes an optional String
        // parameter. For every page after the first, pass the String
        // token returned from the previous page to let it continue
        // from where it left off. For REFRESH, pass null to load the
        // first page.
        val loadKey = when (loadType) {
          LoadType.REFRESH -&gt; null
          // In this example, you never need to prepend, since REFRESH
          // will always load the first page in the list. Immediately
          // return, reporting end of pagination.
          LoadType.PREPEND -&gt; return MediatorResult.Success(
            endOfPaginationReached = true
          )
          // Query remoteKeyDao for the next RemoteKey.
          LoadType.APPEND -&gt; {
            val remoteKey = database.withTransaction {
              remoteKeyDao.remoteKeyByQuery(query)
            }
  
            // You must explicitly check if the page key is null when
            // appending, since null is only valid for initial load.
            // If you receive null for APPEND, that means you have
            // reached the end of pagination and there are no more
            // items to load.
            if (remoteKey.nextKey == null) {
              return MediatorResult.Success(
                endOfPaginationReached = true
              )
            }
  
            remoteKey.nextKey
          }
        }
  
        // Suspending network load via Retrofit. This doesn't need to
        // be wrapped in a withContext(Dispatcher.IO) { ... } block
        // since Retrofit's Coroutine CallAdapter dispatches on a
        // worker thread.
        val response = networkService.searchUsers(query, loadKey)
  
        // Store loaded data, and next key in transaction, so that
        // they're always consistent.
        database.withTransaction {
          if (loadType == LoadType.REFRESH) {
            remoteKeyDao.deleteByQuery(query)
            userDao.deleteByQuery(query)
          }
  
          // Update RemoteKey for this query.
          remoteKeyDao.insertOrReplace(
            RemoteKey(query, response.nextKey)
          )
  
          // Insert new users into database, which invalidates the
          // current PagingData, allowing Paging to present the updates
          // in the DB.
          userDao.insertAll(response.users)
        }
  
        MediatorResult.Success(
          endOfPaginationReached = response.nextKey == null
        )
      } catch (e: IOException) {
        MediatorResult.Error(e)
      } catch (e: HttpException) {
        MediatorResult.Error(e)
      }
    }
  }
  
  </code></pre>
  <p><strong>LoadType.PREPEND</strong> and <strong>LoadType.APPEND</strong> are constantly calling!</p>