Realm access from incorrect thread
Model:
public class RealmModel {
// private Realm realm;
private GetObservable getDataRetrofit;
Realm backgroundRealm ;
public RealmModel(GetObservable getDataRetrofit) {
//realm = Realm.getDefaultInstance();
this.getDataRetrofit = getDataRetrofit;
backgroundRealm = Realm.getDefaultInstance();
}
public void addListPhoto(final ListApi photoList) {
backgroundRealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(@NonNull Realm realm) {
realm.delete(ListApi.class);
realm.insertOrUpdate(photoList);
}
});
}
public void getRealm() {
backgroundRealm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
ListApi listApi = realm.where(ListApi.class).findFirst();
Flowable<ListApi> flowable = null;
if (listApi != null && listApi.isValid()) {
//flowable=Flowable.just(listApi);
flowable = listApi.asFlowable();
}
getDataRetrofit.getBody(flowable, false);
}
});
}
}
Presenter:
@InjectViewState
public class MainPresenter extends MvpPresenter<GetBodyView> implements GetObservable {
private InternetModel internetModel;
private RealmModel realmModel;
public MainPresenter() {
internetModel = new InternetModel(this);
realmModel = new RealmModel(this);
internetModel.retrofitCall();
}
@Override
public void getBody(Flowable<ListApi> observable, final boolean check) {
if (observable == null) {
getViewState().fail();
Log.d("api","fail");
} else {
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new FlowableSubscriber<ListApi>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ListApi listApi) {
Log.d("api","onNext");
if (check) {
realmModel.addListPhoto(listApi);
getViewState().showRetrofit(listApi.getList());
} else {
getViewState().showRetrofit(listApi.getList());
}
}
@Override
public void onError(Throwable t) {
realmModel.getRealm();
Log.d("error",t.toString());
}
@Override
public void onComplete() {
}
});
}
}
}
Крашится при считывании данных с БД(Realm)
Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
Не понимаю уже как сделать так,чтобы оно не крашилось)