title: Android Paging Library scrolls list to the top while invalidating
body: <p>I am new to the paging libray,  whenever i was invalidating the data source, paging brings the list to the top. I was searching through various blogs they were saying that <code>loadInitial</code>,<code>loadRange</code> should be called from the thread where it was started, But in my case i was using coroutine in my application, But i tried this behaviour without using coroutine everything worked as fine.</p>
  
  <p><strong>DataSource</strong></p>
  
  <pre><code>class TestingDataSource(
      private val testingViewModel: TestingViewModel, private val coroutineScope: CoroutineScope
  ) : PositionalDataSource&lt;TestingData&gt;() {
  
  
      override fun loadInitial(
          params: LoadInitialParams,
          callback: LoadInitialCallback&lt;TestingData&gt;
      ) {
          coroutineScope.launch {
              testingViewModel.testingData(0, params.requestedLoadSize) {
                  callback.onResult(it, 0)
  
              }
          }
      }
  
  
      override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback&lt;TestingData&gt;) {
          coroutineScope.launch {
              testingViewModel.testingData(params.startPosition, params.loadSize) {
                  callback.onResult(it)
              }
          }
      }
  
  }
  </code></pre>
  
  <p><strong>DataSource Factory</strong></p>
  
  <pre><code>class DataSourceFactory(
      private val testingViewModel: TestingViewModel,
      private val coroutineScope: CoroutineScope
  ) : DataSource.Factory&lt;Int, TestingData&gt;() {
  
      val dataSource: MutableLiveData&lt;TestingDataSource&gt; = MutableLiveData()
  
      override fun create(): DataSource&lt;Int, TestingData&gt; {
          return TestingDataSource(testingViewModel, coroutineScope)
              .also {
                  dataSource.postValue(it)
              }
      }
  
      fun getDataSource(): TestingDataSource? {
          return dataSource.value
      }
  }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>class TestingViewModel : ViewModel() {
  
  
      lateinit var dataSourceFactory: DataSourceFactory
      lateinit var dataList: LiveData&lt;PagedList&lt;TestingData&gt;&gt;
  
      var isInvalidate: Boolean = false
  
      val list = mutableListOf&lt;TestingData&gt;()
  
  
      fun dataSource() {
  
          dataSourceFactory = DataSourceFactory(this, viewModelScope)
  
          val config = PagedList.Config.Builder()
              .setPageSize(20)
              .setPrefetchDistance(5)
              .setEnablePlaceholders(false)
              .build()
  
          dataList = LivePagedListBuilder(dataSourceFactory, config)
              .build()
      }
  
      suspend fun testingData(offset: Int, limit: Int, callBack: suspend (List&lt;TestingData&gt;) -&gt; Unit) {
          viewModelScope.launch(Dispatchers.IO) {
              if (isInvalidate) {
                  callBack(list)
              } else {
                  if (offset &gt; 60) {
                      dataSourceFactory.getDataSource()?.invalidate()
                      isInvalidate = true
                  } else {
  
                      val data = getData(offset)
                      list.addAll(data)
                      callBack(data)
                  }
              }
          }
      }
  
      fun getData(offset: Int): List&lt;TestingData&gt; {
          return (offset..offset + 20).map {
              TestingData("Some_data_${it}", it.toString())
          }
      }
  
  }
  </code></pre>
  
  <p>Can anyone suggest me how to achieve this?</p>