领域保留已删除的对象

时间:2017-12-26 17:20:08

标签: android android-fragments realm

我有2个片段 A B ,片段 B 我使用事务删除对象然后我执行getActivity().getSupportFragmentManager().popBackStack();到返回片段 A ,删除我调试的对象后,我发现该对象被正确删除,但当我返回片段 A 时,我发现它,它&#39 ;很奇怪!

这是我的片段 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();
}

1 个答案:

答案 0 :(得分:0)

待办事项

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();
             });