【问题标题】:android -java.io.IOException: Canceled when disposedandroid -java.io.IOException:释放时取消
【发布时间】:2019-09-20 18:15:39
【问题描述】:

我正在使用 android 架构组件,rxjava 并使用 mvvm 结构进行改造,顺便说一下,我是这些领域的新手

我遇到的问题是这个,我在viewmodel中处理了我的一次性用品,问题是这个,当我从一个活动转到另一个活动或关闭应用程序时它会被触发,它不会再次重新启动它重新打开活动,所以我失去了连接,我得到了 FAILED: java.io.IOException: Canceled error when I want to use the api connection again 。

这是我的代码:

class CategoryViewModel(private val model:CategoryModel): ViewModel() {

private lateinit var catsLiveData:MutableLiveData<MutableList<Cat>>

fun getCats():MutableLiveData<MutableList<Cat>>{
    if(!::catsLiveData.isInitialized){
        catsLiveData=model.getCats()
    }
    return catsLiveData;
}

override fun onCleared() {
    super.onCleared()
    model.clearDisposable()
}

这是我从互联网获取数据的模型类:

class CategoryModel(
    private val netManager: NetManager,
    private val sharedPrefManager: SharedPrefManager) {

private lateinit var categoryDao: CategoryDao
private lateinit var dbConnection: DbConnection
private lateinit var lastUpdate: LastUpdate
private var list: MutableLiveData<MutableList<Cat>> = MutableLiveData()
public val compositeDisposable= CompositeDisposable()

fun getCats(): MutableLiveData<MutableList<Cat>> {

        return getCatsOnline();


}

private fun getCatsOnline(): MutableLiveData<MutableList<Cat>> {
    Log.v("this", "online ");
    val getCats = ApiConnection.client.create(Category::class.java)
    compositeDisposable+=getCats.getCats(sharedPrefManager.getUid(), lastUpdate.getLastCatDate())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    { success ->
                        list += success.cats

                    }, { error ->
                Log.v("this", "ErrorGetCats " + error.localizedMessage);
            }
            )

    return list;
}


fun clearDisposable(){
    if(!compositeDisposable.isDisposed){
        compositeDisposable.dispose()
        Log.v("this","disposable called");
    }
}
}

这有什么问题?

【问题讨论】:

  • 仅在您的onDestroy 活动中处理一次性用品

标签: android kotlin retrofit rx-java android-architecture-components


【解决方案1】:

处置复合材料将使其处置添加到其中的每个未来Disposable。将其更改为clear,您可以重复使用相同的CompositeDisposable

fun clearDisposable(){
    compositeDisposable.clear()
    Log.v("this","disposable called");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多