原因:java.lang.RuntimeException:java.lang.InterruptedException

原因:java.lang.RuntimeException:java.lang.InterruptedException,java,android,kotlin,rx-java2,interrupted-exception,Java,Android,Kotlin,Rx Java2,Interrupted Exception,使用实现“io.reactivex.rxjava2:rxjava:2.1.9” 我正在尝试用rxJava解析。解析长json数据。所以我的解析需要时间。 但若用户离开屏幕,我需要完成解析工作,但我的应用程序却崩溃了。 用于分析的方法: override fun restore(): Observable<List<Pair<String, String>?>> { return backupRemoteSource.getBackup()

使用实现“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
                        })
                    }

                }
....}
但在长时间数据解析期间调用时:

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)

哪里错了?

刚刚使用的
emitter.tryOnError

   try{

    ....

    } catch (error: InterruptedException) {
         emitter.tryOnError(BackupException(error.message ?: "unknown exception"))
    }

刚刚使用的
emitter.tryOnError

   try{

    ....

    } catch (error: InterruptedException) {
         emitter.tryOnError(BackupException(error.message ?: "unknown exception"))
    }