I want to post each RealmResults data to a REST endpoint, and want to delete its data if sending is success.
Running following code, success sending but fail to delete.
I tried to use target.deleteFromRealm()
in Response()
but IllegalStateException occurred.
java.lang.IllegalStateException: Realm access from incorrect thread.
Realm objects can only be accessed on the thread they were created.
How can I delete target
?
(using Realm Java 3.1.2 and Retrofit 2.2.0)
RealmResults<Model> results = realm.where(Model.class).findAll();
for ( final Model target: results ){
Call<Void> task = restInterface.post(gson.toJson(target));
task.enqueue( new CallBack<Void>(){
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
// ?? how to delete target from realm ??
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
// do nothing
}
});
}