Comments (4)
很感谢你的回复,我又翻了一下源码,在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.
HomeArtRemoteMediator
内部的数据最终通过 flow
返回,异常处理在 flow
的 catch
处理。具体看 HomeFragment
59 行
from coroutineswanandroid.
感谢回复,但是我测试了一下,在HomeArtRemoteMediator
抛出的异常在HomeArticleFragment
的第60行的catch
并不会捕获,并且删除这个catch
也并不会崩溃。
from coroutineswanandroid.
这个问题我又重新看了下代码的实现,梳理了下大概的逻辑。在 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)
- 一点问题和建议?希望大佬指点! HOT 1
- UI bug
- 请求优化
- 运行报这个错误 HOT 4
- 缺少class RequestStatusCode in version 2.0.2 HOT 4
- 关于异常 HOT 1
- 来个java篇好不好 HOT 2
- 使用AsyncPagedListDiffer解决刷新空白的问题 HOT 7
- Didn't find class on path DexPathList HOT 2
- Coroutine和组件结合的生命周期不用管理的么? HOT 1
- 一加5T上安装了以后无法启动app HOT 1
- 体验APK在Android R下面崩溃 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coroutineswanandroid.