title: How to add Item in PagedList of Paging Library
body: <p>I am trying to add comment functionality in my App. Just like Instagram or Facebook where comment will be added on comment list after being successfully saved to the database. I am using <a href="https://developer.android.com/topic/libraries/architecture/" rel="nofollow noreferrer">Android Architecture Components</a> (ViewModel, LiveData and Paging Library).</p>
  
  <p>Here is my ViewModel where I think I should update my PagedList</p>
  
  <pre><code>class CommentsViewModel : ViewModel() {
  lateinit var networkState: LiveData&lt;NetworkState&gt;
  private val executor: Executor
  private lateinit var tDataSource: LiveData&lt;CommentsDataSource&gt;
  private lateinit var dataFactory: CommentsDataFactory
  private var repository: CommentsRepository
  private var commentsList: LiveData&lt;PagedList&lt;Comment&gt;&gt;? = null
  
  init {
      executor = Executors.newFixedThreadPool(5)
      repository = CommentsRepository()
  }
  
  fun getComments(postId: Int): LiveData&lt;PagedList&lt;Comment&gt;&gt;? {
      dataFactory = CommentsDataFactory(executor, postId)
  
      tDataSource = dataFactory.mutableLiveData
  
      networkState = Transformations.switchMap(dataFactory.mutableLiveData) {
          it.networkState
      }
  
      val pagedListConfig = PagedList.Config.Builder()
              .setEnablePlaceholders(true)
              .setInitialLoadSizeHint(Constants.INITIAL_LOAD)
              .setPageSize(Constants.PAGE_SIZE)
              .build()
  
      commentsList = LivePagedListBuilder(dataFactory, pagedListConfig)
              .setFetchExecutor(executor)
              .build()
      return commentsList
  }
  
  //This method here brings errors.
  fun addComment(comment: Comment) {
      commentsList?.value?.add(comment)
  }
  
  fun postComment(map: Map&lt;String, String&gt;): LiveData&lt;NetworkState&gt; {
      repository.postComment(map)
      return repository.mNetworkState
  }}
  </code></pre>
  
  <p>How to do it Correctly?.</p>
  
  <p><strong>CommentsDataFactory</strong></p>
  
  <pre><code>class CommentsDataFactory(private val executor: Executor, private val postId: Int)
  : DataSource.Factory&lt;Int, Comment&gt;() {
  
  val mutableLiveData = MutableLiveData&lt;CommentsDataSource&gt;()
  
  override fun create(): DataSource&lt;Int, Comment&gt;? {
      val dataSource = CommentsDataSource(executor, postId)
      mutableLiveData.postValue(dataSource)
      return dataSource
  }}
  </code></pre>
  
  <p><strong>CommentsDataSource</strong></p>
  
  <pre><code>class CommentsDataSource(private val retryExecutor: Executor, private val post_id: Int)
  : PageKeyedDataSource&lt;Int, Comment&gt;() {
  
  private var retry: (() -&gt; Any)? = null
  private val apiClient = getClient()
  
  val networkState = MutableLiveData&lt;NetworkState&gt;()
  
  val initialLoad = MutableLiveData&lt;NetworkState&gt;()
  
  fun retryAllFailed() {
      val prevRetry = retry
      retry = null
      prevRetry?.let {
          retryExecutor.execute {
              it.invoke()
          }
      }
  }
  
  override fun loadBefore(
          params: LoadParams&lt;Int&gt;,
          callback: LoadCallback&lt;Int, Comment&gt;) {
  }
  
  override fun loadAfter(params: LoadParams&lt;Int&gt;, callback: LoadCallback&lt;Int, Comment&gt;) {
      networkState.postValue(NetworkState.LOADING)
      apiClient.getComments(post_id, params.key).enqueue(
              object : retrofit2.Callback&lt;List&lt;Comment&gt;&gt; {
                  override fun onFailure(call: Call&lt;List&lt;Comment&gt;&gt;, t: Throwable) {
                      retry = {
                          loadAfter(params, callback)
                      }
                      networkState.postValue(NetworkState.error(t.message ?: "unknown err"))
                  }
  
                  override fun onResponse(
                          call: Call&lt;List&lt;Comment&gt;&gt;,
                          response: Response&lt;List&lt;Comment&gt;&gt;) {
                      when {
                          response.isSuccessful -&gt; {
                              retry = null
                              callback.onResult(response.body() as MutableList&lt;Comment&gt;, params.key + 10)
                              networkState.postValue(NetworkState.LOADED)
                          }
                          response.code() == 404 -&gt; networkState.postValue(NetworkState.END)
                          else -&gt; {
                              retry = {
                                  loadAfter(params, callback)
                              }
                              networkState.postValue(
                                      NetworkState.error("error code: ${response.code()}"))
                          }
                      }
                  }
              }
      )
  }
  
  override fun loadInitial(
          params: LoadInitialParams&lt;Int&gt;,
          callback: LoadInitialCallback&lt;Int, Comment&gt;) {
  
      val request = apiClient.getComments(post_id, 0)
  
      networkState.postValue(NetworkState.LOADING)
      initialLoad.postValue(NetworkState.LOADING)
  
      try {
          retry = null
          val response = request.execute()
  
          when {
              response.isSuccessful -&gt; {
                  networkState.postValue(NetworkState.LOADED)
                  initialLoad.postValue(NetworkState.LOADED)
                  callback.onResult(response.body() as MutableList&lt;Comment&gt;, 0, 10)
              }
              response.code() == 404 -&gt; networkState.postValue(NetworkState.END)
              else -&gt; networkState.postValue(
                      NetworkState.error("error code: ${response.code()}"))
          }
  
      } catch (ioException: IOException) {
          retry = {
              loadInitial(params, callback)
          }
          val error = NetworkState.error(ioException.message ?: "unknown error")
          networkState.postValue(error)
          initialLoad.postValue(error)
      }
  }}
  </code></pre>