I have following Base class for my ViewModels :
abstract class BaseViewModel<T, R>(private val schedulerProvider: BaseSchedulerProvider) :
ViewModel() {
private val compositeDisposable = CompositeDisposable()
private val _liveData = MutableLiveData<Resource<T>>()
val liveData: LiveData<Resource<T>>
get() = _liveData
protected abstract val requestObservable: Observable<R>
protected abstract fun getSuccessResult(it: R): T
init {
Handler().postDelayed({
sendRequest()
}, 1)
}
fun sendRequest() {
_liveData.value = Resource.Loading()
composeObservable { requestObservable }
.subscribe({
_liveData.postValue(Resource.Success(getSuccessResult(it)))
}) {
_liveData.postValue(Resource.Failure(it.localizedMessage))
Timber.e(it)
}.also { compositeDisposable.add(it) }
}
private fun <T> composeObservable(task: () -> Observable<T>): Observable<T> = task()
.doOnSubscribe { EspressoIdlingResource.increment() } // App is busy until further notice
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.doFinally {
if (!EspressoIdlingResource.countingIdlingResource.isIdleNow) {
EspressoIdlingResource.decrement() // Set app as idle.
}
}
Here is How I initialize ViewModel in Fragment :
class MainFragment @Inject
constructor() // Required empty public constructor
: DaggerFragment() {
@Inject
lateinit var factory: MainViewModel.Factory
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
val viewModel = ViewModelProvider(this, factory).get(MainViewModel::class.java)
}
As you see I have following in init block to delay :
Handler().postDelayed({
sendRequest()
}, 1)
Without this delay app crashes since api will be null in the inherited class :
class MainViewModel(
api: PokemonService,
schedulerProvider: BaseSchedulerProvider
) : BaseViewModel<List<Pokemon>, List<NamedResponseModel>>(schedulerProvider) {
override val requestObservable: Observable<List<NamedResponseModel>> =
api.getPokemonList(LIMIT).map { it.results }
}
If I do not use generic parameterized ViewModel, there is no need for delay. You can find repository at : https://github.com/Ali-Rezaei/Pokemon
Why do I need a delay, and what is the solution to avoid delay?
question from:
https://stackoverflow.com/questions/65926403/generic-parameterized-base-viewmodel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…