title: Android Paging 3 with Room
body: <p>I'm migrating from Paging 2 to Paging 3.  The app stores a large dataset in a database using Room, and I can load the data from Room and display it okay. The issue I have is as soon as the app makes a change to the database, it crashes.</p>
  <p><strong>Code Snippets</strong><br />
  <code>IssueRepository</code></p>
  <pre><code>@Query(&quot;SELECT * FROM isssue WHERE project_id = ?&quot;)
  fun findAllInProject(projectId:Int): PagingSource&lt;Int, IssueListBean&gt;
  </code></pre>
  <p>In the function <code>onCreateView</code></p>
  <pre><code>val dataSource = DB.store.issueRepository().findAllInProject(project.id)
  val pageConfig = PagingConfig(50)
  val pager = Pager(pageConfig, null) { dataSource }.flow
  viewLifecycleOwner.lifecycleScope.launchWhenCreated {
      pager.collectLatest { data -&gt;
          adapter.submitData(data)
      }
  }
  </code></pre>
  <pre><code>class PagingAdapter : PagingDataAdapter&lt;IssueListBean, PagingAdapter.ViewHolder&gt;(EntityComparator()) {
      inner class ViewHolder(private val adapterBinding: ItemIssueListBinding) : RecyclerView.ViewHolder(adapterBinding.root) {
          fun bind(position: Int) {
              val issueListBean = getItem(position)
              adapterBinding.label.text = issueListBean.label
          }
      }
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
          val binding = ItemIssueListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
          return ViewHolder(binding)
      }
  
      override fun onBindViewHolder(holder: ViewHolder, position: Int) {
          holder.bind(position)
      }
  }
  </code></pre>
  <p>So when users tap on an item they can edit it. As soon the item is saved via Room the app crashes with the following exception:</p>
  <pre><code>java.lang.IllegalStateException: An instance of PagingSource was re-used when Pager expected to create a new
      instance. Ensure that the pagingSourceFactory passed to Pager always returns a
      new instance of PagingSource.
  </code></pre>
  <p>Am I using Paging 3 wrong? I can't find many articles online talk about using Room as data source where you make changes.</p>