我想将每个 RealmResults 数据发布到一个 REST 端点,如果发送成功,我想删除它的数据。
运行以下代码,发送成功但删除失败。
我尝试使用target.deleteFromRealm()
inResponse()
但发生了 IllegalStateException。
java.lang.IllegalStateException: Realm access from incorrect thread.
Realm objects can only be accessed on the thread they were created.
我怎样才能删除target
?(使用 Realm Java 3.1.2 和 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
}
});
}