I'm using android architecture component ,rxjava and retrofit with mvvm structure , i'm new in these fields by the way
the problem I've is this , I'm disposed my disposables in viewmodel ,the problem is this , it'll triggered when I go from activity to activity or close the application and it'll not restart again when it reopens the activity, so I lost the connection and I get FAILED: java.io.IOException: Canceled error when I want to use the api connection again .
this is my code:
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()
}
this is my model class where I get the data from internet :
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");
}
}
}
what is wrong with this ?
from android -java.io.IOException: Canceled when disposed
No comments:
Post a Comment