GithubHelp home page GithubHelp logo

Comments (4)

ycuwq avatar ycuwq commented on August 22, 2024 1

很感谢你的回复,我又翻了一下源码,在3.0.0-alpha04这里我没有看到有捕获异常,如果利用了MediatorResult.Error(e)处理问题应该会在addLoadStateListener看到,我这边复现的报错的异常栈是协程调用相关的问题。
我又翻了下源码和googlecodelabs的sample。发现原sample在paging版本为3.0.0-alpha02不会出现额外调用的LoadType.PREPEND和LoadType.APPEND`导致不会抛出异常,但是在后续的版本还是会有异常。在googlecodelabs里面也有相同的issues

然后我就看到你的项目里面使用了高版本的paging虽然会抛出InvalidObjectException但是程序没有崩溃,所以比较好奇。我目前对协程和flow的了解还不是特别深入,所以这个问题的原因没有没有发现,但是我推测是协程的版本不同导致的处理方式不同。

这个问题我又重新看了下代码的实现,梳理了下大概的逻辑。在 load 的最后数据返回的时候,其实有通过 try..catch.. 进行异常捕获,捕获到了异常,通过 MediatorResult.Error(e) 进行处理。
接着可以看下 PageFetcherSnapshot.kt 文件内部的 doBoundaryCall 这个方法,这里对 MediatorResult.Error 进行了处理

when (val mediatorResult = load(coroutineScope, loadType, pagingState)) {
            is RemoteMediator.MediatorResult.Error -> {
                stateLock.withLock {
                    val errorState = Error(mediatorResult.throwable)
                    if (state.loadStates.set(loadType, true, errorState)) {
                        pageEventCh.send(LoadStateUpdate(loadType, true, errorState))
                    }
                }
            }
...
}

到这呢,就把抛出的异常消费了,也就不会出现崩溃的问题了

from coroutineswanandroid.

kukyxs avatar kukyxs commented on August 22, 2024

HomeArtRemoteMediator 内部的数据最终通过 flow 返回,异常处理在 flowcatch 处理。具体看 HomeFragment 59 行

from coroutineswanandroid.

ycuwq avatar ycuwq commented on August 22, 2024

感谢回复,但是我测试了一下,在HomeArtRemoteMediator抛出的异常在HomeArticleFragment的第60行的catch并不会捕获,并且删除这个catch也并不会崩溃。

from coroutineswanandroid.

kukyxs avatar kukyxs commented on August 22, 2024

这个问题我又重新看了下代码的实现,梳理了下大概的逻辑。在 load 的最后数据返回的时候,其实有通过 try..catch.. 进行异常捕获,捕获到了异常,通过 MediatorResult.Error(e) 进行处理。
接着可以看下 PageFetcherSnapshot.kt 文件内部的 doBoundaryCall 这个方法,这里对 MediatorResult.Error 进行了处理

when (val mediatorResult = load(coroutineScope, loadType, pagingState)) {
            is RemoteMediator.MediatorResult.Error -> {
                stateLock.withLock {
                    val errorState = Error(mediatorResult.throwable)
                    if (state.loadStates.set(loadType, true, errorState)) {
                        pageEventCh.send(LoadStateUpdate(loadType, true, errorState))
                    }
                }
            }
...
}

到这呢,就把抛出的异常消费了,也就不会出现崩溃的问题了

from coroutineswanandroid.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.