مطابق ویدیو آموزشی پیش رفتم ولی وقتی دیتا رو قطع کردم ارور در لاگ کت نمایش داده نشد
package com.sevenlearn.mvvmproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainVewModel mainVewModel= new ViewModelProvider(this,new MainViewModelFactory(new StudentRepository(ApiServiceProvider.getApiService(),AppDatabase.getInstance(getApplicationContext()).studentDao()))).get(MainVewModel.class);
mainVewModel.getStudents().observe(this,students->{
Log.i("mainactivity", "onCreate: ");
});
mainVewModel.getError().observe(this,error->{
Log.e("mainactivity", "Error: "+error );
});
}
}
--------------------
package com.sevenlearn.mvvmproject;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import java.util.List;
import io.reactivex.CompletableObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class MainVewModel extends ViewModel {
private ApiService apiService;
private MutableLiveData<String> error=new MutableLiveData<>();
private StudentRepository repository;
private Disposable disposable;
public MainVewModel(StudentRepository repository){
this.repository = repository;
repository.refreshStudents()
.subscribeOn(Schedulers.io())
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
disposable=d;
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
error.postValue("خطای نامشخص");
}
});
}
public LiveData<List<Student>> getStudents() {
return repository.getStudent();
}
public LiveData<String> getError() {
return error;
}
@Override
protected void onCleared() {
disposable.dispose();
super.onCleared();
}
}