【问题标题】:Android paging 3 library removing .cahcedIn(viewModelScope) will throw exception删除 .cahcedIn(viewModelScope) 的 Android 分页 3 库将引发异常
【发布时间】:2020-12-22 11:11:20
【问题描述】:

我正在我的应用程序中使用分页 3.0.0-alpha11 库。
有一个片段有一个视图模型。在视图模型中,我实现了这样的分页数据。名为 promoId 的参数有一个可变的实时数据。 promoId 是一个字符串,表示模型的 id。我将它与另一个名为 page 的参数一起使用以从网络获取一页数据。所以问题是我第一次导航到片段时一切正常,但是在弹出片段并第二次导航到它之后,我收到错误并且应用程序崩溃。
我的片段代码。

class ExploreFragment : DaggerFragment() {

    private var _binding: FragmentExploreBinding? = null
    private val binding get() = _binding!!

    @Inject
    lateinit var viewModelProviderFactory: ViewModelProviderFactoryImpl
    private lateinit var exploreViewModel: ExploreViewModel

    
    lateinit var commentsAdapter: LiveCommentAdapter

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initLists()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
       
        exploreViewModel =
            ViewModelProvider(this, viewModelProviderFactory)[ExploreViewModel::class.java]

        observeComments()
        commentsViewModel.getPromoComments(promoId)
        }
    }

    private fun initLists() {
    
        commentsAdapter = LiveCommentAdapter()
        binding.rvComments.adapter = commentsAdapter
        binding.rvSearchResults.adapter = searchResultAdapter
    }
   

    private fun observeComments() {
        exploreViewModel.commentsLiveData.observe(viewLifecycleOwner) { comments ->
            commentsAdapter.submitData(viewLifecycleOwner.lifecycle, comments)
        }
    }

   
 

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

这是我的视图模型
另请注意,我在commentRepository.getCommentsByPromo(promoId?:"") 之后没有使用cachedIn(viewModelScope),因为它会缓存数据并且不会第二次调用分页数据的加载方法。

class ExploreViewModel @Inject constructor(
    @Inject @JvmField var commentRepository: CommentRepository,
) :
    ViewModel() {


    private val promoIdLiveData: MutableLiveData<String?> = MutableLiveData()

    var commentsLiveData: LiveData<PagingData<Comment>> = promoIdLiveData.switchMap { promoId ->
        commentRepository.getCommentsByPromo(promoId?:"")
    }

    fun getComments(promoId: String) {
        promoIdLiveData.value = promoId
    }
}

这是我的存储库代码。

class CommentRepository @Inject constructor(@Inject @JvmField var retrofit: Retrofit) {

    private val api: CommentsApi = retrofit.create(CommentsApi::class.java)

    fun getCommentsByPromo(promoId: String):LiveData<PagingData<Comment>> = Pager(
        config = PagingConfig(pageSize = 20, enablePlaceholders = false,prefetchDistance = 100),
        pagingSourceFactory = { PromoCommentPagingSource(promoId, api) }
    ).liveData

}

我得到的错误是

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.tivasoft.project, PID: 28896
    java.lang.IllegalStateException: Attempt to collect twice from pageEventFlow, which is an illegal operation. Did you forget to call Flow<PagingData<*>>.cachedIn(coroutineScope)?
        at androidx.paging.PageFetcherSnapshot$pageEventFlow$1.invokeSuspend(PageFetcherSnapshot.kt:88)
        at androidx.paging.PageFetcherSnapshot$pageEventFlow$1.invoke(Unknown Source:10)
        at androidx.paging.CancelableChannelFlowKt$cancelableChannelFlow$1.invokeSuspend(CancelableChannelFlow.kt:35)
        at androidx.paging.CancelableChannelFlowKt$cancelableChannelFlow$1.invoke(Unknown Source:10)
        at kotlinx.coroutines.flow.ChannelFlowBuilder.collectTo$suspendImpl(Builders.kt:344)
        at kotlinx.coroutines.flow.ChannelFlowBuilder.collectTo(Unknown Source:0)
        at kotlinx.coroutines.flow.internal.ChannelFlow$collectToFun$1.invokeSuspend(ChannelFlow.kt:60)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:69)
        at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:349)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:27)
        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:49)
        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1)
        at androidx.paging.AsyncPagingDataDiffer.submitData(AsyncPagingDataDiffer.kt:156)
        at androidx.paging.PagingDataAdapter.submitData(PagingDataAdapter.kt:172)
        at com.tivasoft.project.ui.explore.ExploreFragment$observeComments$$inlined$observe$1.onChanged(LiveData.kt:52)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:144)
        at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:443)
        at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:395)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
        at androidx.fragment.app.FragmentViewLifecycleOwner.handleLifecycleEvent(FragmentViewLifecycleOwner.java:51)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:2737)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:365)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1194)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

如果有人能告诉我哪里出了问题,我将非常感激

【问题讨论】:

  • here 的 2 个答案帮助我摆脱了这个错误

标签: android kotlin android-jetpack android-paging


【解决方案1】:

我对你想要发生的事情有点困惑,但我的理解是你想在导航回来时从头开始重新加载。

错误是说你不能在PagingData 的同一个实例上调用.submitData 两次。您实际上是在无意中尝试加载缓存数据,并且由于您没有调用 cachedIn,因此 Paging 会引发异常。

强制 Paging 每次都从头开始加载的一种简单方法是确保每次都构造一个 Pager 的新实例。

所以我会尝试在onViewCreated 中调用observeComments() 并将commentsLiveData 转换为一个函数(而不是一个var),每次调用它时都会返回一个新的LiveData&lt;PagingData&gt;

【讨论】:

    【解决方案2】:

    在我的情况下,我有一个 Transformations.switchMap(liveData),这导致了问题,解决方法是让 PagingDataswitchMap 之前缓存

    fun observable(): LiveData<PagingData<SomeModel>> {
        val pagingData = //obtained somewhere
        //make sure to cache it
        val chachedPagingData = pagingData.cachedIn(viewModelScope)
        return Transformations.switchMap(otherLiveData) { observed ->
            //use observed for obtaining live attributes needed
            chachedPagingData.filter {...}.map{...}
        }
    }
    

    对我来说,这就是解决方案,因为 pagingData 有时可以是 suspend,所以上面的解释会像这样:

    suspend fun observable(): LiveData<PagingData<SomeModel>> {
        val someAttributeFromTheDb = //get it using ROOM
        val pagingData = if (someAttributeFromTheDb == /*condition*/) {
            suspendPagingData } else regularPagingData
        val chachedPagingData = ...
    }
    

    【讨论】:

      【解决方案3】:

      只需替换 MutableLiveData

      private val promoIdLiveData: MutableLiveData<String?> = MutableLiveData()
      

      使用 SingleLiveEvent

      private val promoIdLiveData: SingleLiveEvent<String?> = SingleLiveEvent()
      

      SingleLiveEvent

      class SingleLiveEvent<T> : MutableLiveData<T>() {
      private val pending = AtomicBoolean(false)
      @MainThread
      override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
          if (hasActiveObservers()) {
              Log.w(TAG, "Multiple observers registered but only one will be notified of changes.")
          }
          // Observe the internal MutableLiveData
          super.observe(
              owner,
              Observer { t ->
                  if (pending.compareAndSet(true, false)) {
                      observer.onChanged(t)
                  }
              }
          )
      }
      
      @MainThread
      override fun setValue(t: T?) {
          pending.set(true)
          super.setValue(t)
      }
      /**
       * Used for cases where T is Void, to make calls cleaner.
       */
      @MainThread
      fun call() {
          value = null
      }
      companion object {
          private val TAG = "SingleLiveEvent"
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-14
        • 2020-11-01
        • 2013-05-20
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 2017-02-12
        • 2010-12-03
        相关资源
        最近更新 更多