原因:java.lang.RuntimeException:java.lang.InterruptedException
Caused by: java.lang.RuntimeException: java.lang.InterruptedException
使用实现'io.reactivex.rxjava2:rxjava:2.1.9'
我正在尝试使用 rxJava 进行解析。解析长 json 数据。所以我的解析需要时间。
但是,如果用户离开屏幕,我的解析需要完成工作,但我的应用程序会崩溃。
解析方法:
override fun restore(): Observable<List<Pair<String, String>?>> {
return backupRemoteSource.getBackup()
.flatMap { urlBackup ->
Observable.create<Boolean> { emitter ->
var isRestore = true
try {
val url = URL(urlBackup)
url.openConnection()
InputStreamReader(url.openStream(), "UTF-8").use {
val jsonReader = JsonReader(it)
jsonReader.beginArray()
var tour: Tour? = null
/* tours */
while (jsonReader.hasNext() ) {
/* tour item*/
jsonReader.beginObject()
while (jsonReader.hasNext() && isRestore) {
val name = jsonReader.nextName()
when (name) {
"tourInfo" -> {
tour = tourMapper.fromRx(gsonParser.fromJson<TourBackup>(jsonReader, TourBackup::class.java))
.flatMap {
tourLocalSource.save(it)
}.blockingFirst()
Log.i(tag, "\n@saved tour")
}
jsonReader.endArray()
}
}
}
jsonReader.endObject()
}
jsonReader.endArray()
emitter.onNext(true)
}
} catch (error: InterruptedException) {
emitter.onError(BackupException(error.message ?: "unknown exception"))
} catch (error: NoSuchFileException) {
emitter.onError(BackupException(error.message ?: "unknown exception"))
} catch (error: IOException) {
emitter.onError(BackupException(error.message ?: "unknown exception"))
}
emitter.setCancellable({
Log.d(tag,"cancel restore")
isRestore = false
})
}
}
....}
然后我打电话给:
fun restore() {
disposables.add(backUpRepo.restore()
.compose(RxUtils.ioToMainTransformer())
.subscribe()
}
但是在长数据解析过程中调用时:
disposables.dispose()
我崩溃了:
04-23 14:57:27.456 28001-28063/com.jellyworkz.udark.debug E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1
Process: com.jellyworkz.udark.debug, PID: 28001
io.reactivex.exceptions.UndeliverableException: java.lang.RuntimeException: java.lang.InterruptedException
....
Caused by: java.lang.RuntimeException: java.lang.InterruptedException
at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:45)
at io.reactivex.internal.observers.BlockingBaseObserver.blockingGet(BlockingBaseObserver.java:74)
at io.reactivex.Observable.blockingFirst(Observable.java:4987)
at com.jellyworkz.udark.backup.source.BackupRepositoryImpl$restore.subscribe(BackupRepositoryImpl.kt:113)
我哪里错了?
刚用过emitter.tryOnError
try{
....
} catch (error: InterruptedException) {
emitter.tryOnError(BackupException(error.message ?: "unknown exception"))
}
使用实现'io.reactivex.rxjava2:rxjava:2.1.9'
我正在尝试使用 rxJava 进行解析。解析长 json 数据。所以我的解析需要时间。 但是,如果用户离开屏幕,我的解析需要完成工作,但我的应用程序会崩溃。 解析方法:
override fun restore(): Observable<List<Pair<String, String>?>> {
return backupRemoteSource.getBackup()
.flatMap { urlBackup ->
Observable.create<Boolean> { emitter ->
var isRestore = true
try {
val url = URL(urlBackup)
url.openConnection()
InputStreamReader(url.openStream(), "UTF-8").use {
val jsonReader = JsonReader(it)
jsonReader.beginArray()
var tour: Tour? = null
/* tours */
while (jsonReader.hasNext() ) {
/* tour item*/
jsonReader.beginObject()
while (jsonReader.hasNext() && isRestore) {
val name = jsonReader.nextName()
when (name) {
"tourInfo" -> {
tour = tourMapper.fromRx(gsonParser.fromJson<TourBackup>(jsonReader, TourBackup::class.java))
.flatMap {
tourLocalSource.save(it)
}.blockingFirst()
Log.i(tag, "\n@saved tour")
}
jsonReader.endArray()
}
}
}
jsonReader.endObject()
}
jsonReader.endArray()
emitter.onNext(true)
}
} catch (error: InterruptedException) {
emitter.onError(BackupException(error.message ?: "unknown exception"))
} catch (error: NoSuchFileException) {
emitter.onError(BackupException(error.message ?: "unknown exception"))
} catch (error: IOException) {
emitter.onError(BackupException(error.message ?: "unknown exception"))
}
emitter.setCancellable({
Log.d(tag,"cancel restore")
isRestore = false
})
}
}
....}
然后我打电话给:
fun restore() {
disposables.add(backUpRepo.restore()
.compose(RxUtils.ioToMainTransformer())
.subscribe()
}
但是在长数据解析过程中调用时:
disposables.dispose()
我崩溃了:
04-23 14:57:27.456 28001-28063/com.jellyworkz.udark.debug E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-1
Process: com.jellyworkz.udark.debug, PID: 28001
io.reactivex.exceptions.UndeliverableException: java.lang.RuntimeException: java.lang.InterruptedException
....
Caused by: java.lang.RuntimeException: java.lang.InterruptedException
at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:45)
at io.reactivex.internal.observers.BlockingBaseObserver.blockingGet(BlockingBaseObserver.java:74)
at io.reactivex.Observable.blockingFirst(Observable.java:4987)
at com.jellyworkz.udark.backup.source.BackupRepositoryImpl$restore.subscribe(BackupRepositoryImpl.kt:113)
我哪里错了?
刚用过emitter.tryOnError
try{
....
} catch (error: InterruptedException) {
emitter.tryOnError(BackupException(error.message ?: "unknown exception"))
}