使用实现'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$1$1.subscribe(BackupRepositoryImpl.kt:113)
我哪里错了?