从RealmRecyclerViewAdapter中删除Realm对象

时间:2016-11-23 13:03:11

标签: android realm

我得到了很多IllegalStateExceptions,我很难调试。 它们是在我从RealmAdapter中删除Realm对象时发生的。 对于RealmAdapter,我使用了' io.realm:android-adapters:1.3.0'版本

这是我在适配器中长按时删除对象的方法:

    //Delete message object
    new MaterialDialog.Builder(holder.cardView.getContext()).title("Delete")
            .content("Delete message?")
            .positiveText("YES")
            .negativeText("Cancel")
            .onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                    //Call delete
                    ApiManager.ApiManagerService apiManagerService = ApiManager.createService(ApiManager.ApiManagerService.class);
                    apiManagerService.deleteMessage(message.getId()).enqueue(new Callback<Void>() {
                        @Override
                        public void onResponse(Call<Void> call, Response<Void> response) {
                            Logger.d("delete message onResponse callback");

                            //Delete from realm
                            Realm realm = Realm.getDefaultInstance();
                            realm.beginTransaction();
                            try {
                                message.deleteFromRealm();
                            } catch(Exception e) {
                                e.printStackTrace();
                            }
                            realm.commitTransaction();

                            //Notify adapter
                            notifyDataSetChanged();
                        }

                        @Override
                        public void onFailure(Call<Void> call, Throwable t) {
                            Logger.d("delete message onFailure callback");
                        }
                    });
                }
            })
            .show();

    return false;
}

处理从适配器内删除对象的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

                    @Override
                    public void onResponse(Call<Void> call, Response<Void> response) {
                        Logger.d("delete message onResponse callback");

                        //Delete from realm
                        Realm realm = Realm.getDefaultInstance();
                        realm.beginTransaction();
                        try {
                            message.deleteFromRealm();
                        } catch(Exception e) {
                            e.printStackTrace();
                        }
                        realm.commitTransaction();

                        //Notify adapter
                        notifyDataSetChanged();
                    }

应该是

                    @Override
                    public void onResponse(Call<Void> call, Response<Void> response) {
                        Logger.d("delete message onResponse callback");

                        //Delete from realm
                        final long messageId = message.getId();
                        Realm realm = Realm.getDefaultInstance(); // TODO: use Realm bound to lifecycle
                        realm.executeTransactionAsync(new Realm.Transaction() {
                             @Override
                             public void execute(Realm realm) {
                                 realm.where(Message.class).equalTo("messageId", messageId).findAll().deleteAllFromRealm();
                             }
                        });
                        realm.close();

                        //Notify adapter
                        ////notifyDataSetChanged();
                    }