我有两个片段A和B,在片段BI中使用事务删除一个对象然后我执行getActivity().getSupportFragmentManager().popBackStack();
返回片段A,删除对象后我调试并发现该对象被正确删除,但是当我返回片段AI时发现它,这很奇怪!
这是我的片段A中的代码
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(@NonNull Realm realm) {
Infraction infraction = realm.where(Infraction.class).equalTo("id",id_new_inf).findFirst();
if(infraction != null)
infraction.deleteFromRealm();
}
});
getActivity().getSupportFragmentManager().popBackStack();
那么为什么对象没有被删除?
在片段A中,我没有什么特别的东西,它只是onCreateView中的一个请求
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.list_infractions, container, false);
infractions = realm.where(Infraction.class).findAll();
}
做
realm.executeTransactionAsync((r) ->
Infraction infraction = r.where(Infraction.class).equalTo("id",id_new_inf).findFirst();
if(infraction != null)
infraction.deleteFromRealm();
}
}, () -> { // on Success
getActivity().getSupportFragmentManager()
.popBackStack();
});