如何使用 for 循环删除特定领域对象

How to delete specific realm object using for loop

我目前正在尝试使用 for 循环删除模型中的特定领域对象,

但每次我执行 deleteFromRealm(i) 它都会停止循环,我无法再删除其他对象。

不过我还没有尝试过任何其他选项。

final Realm realms = Realm.getDefaultInstance();
        realms.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<CashCountScoreModel> cashCountScoreModels =
                        CashCountScoreModel.getAll(realm);
                for (int i = 0; i < cashCountScoreModels.size(); i++) {
                    if (cashCountScoreModels.get(i) != null && cashCountScoreModels.get(i).isCashOnHand) {
                        Log.d("CheckName : pos -- ",  i +"~~" + cashCountScoreModels.get(i).isCashOnHand);
                        Log.d("CheckName : pos --",  i + "~~" + cashCountScoreModels.get(i).employeeName);
                        cashCountScoreModels.deleteFromRealm(i);
                    //    continue;
                    }
                }
            }
        });

每当我尝试 运行 应用程序并执行此特定代码 cashCountScoreModels.deleteFromRealm(i); 时,它会停止循环。

不确定您使用的是哪个版本的 Realm。但是从 3.0.0 开始,Realm 集合是实时的,因此会立即更新。因此,cashCountScoreModels.size() 将在每次删除时减少 return 计数。就您而言,我怀疑您的集合中只有 2 个条目。您可能想改用 OrderedRealmCollectionSnapshot。试试下面的代码。

final Realm realms = Realm.getDefaultInstance();
        realms.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<CashCountScoreModel> cashCountScoreModels = CashCountScoreModel.getAll(realm);
                OrderedRealmCollectionSnapshot snapshot = cashCountScoreModels.createSnapshot();
                for (CashCountScoreModel cashCountScoreModel : snapshot) {
                    if (cashCountScoreModel != null && cashCountScoreModel.isCashOnHand) {
                        Log.d("CheckName : pos -- ",  i +"~~" + cashCountScoreModel.isCashOnHand);
                        Log.d("CheckName : pos --",  i + "~~" + cashCountScoreModel.employeeName);
                        cashCountScoreModel.deleteFromRealm();
                    }
                }
            }
        });

https://realm.io/docs/java/latest/ or the documentation available at https://realm.io/docs/java/3.0.0/api/io/realm/OrderedRealmCollection.html#loops 上寻找 Iterations & snapshots 以了解集合中的更多实时更新和 OrderedRealmCollectionSnapshot

发生这种情况是因为我认为您想从单个执行块中删除多个领域对象。 在执行块中尝试以下代码。

RealmResults<CashCountScoreModel> cashCountScoreModels=realm.where(CashCountScoreModel.class).equalTo(CashCountScoreModel.isCashOnHand,true).findAll();
cashCountScoreModels.deleteAllFromRealm();
realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
RealmResults<CashCountScoreModel> cashCountScoreModels=realm.where(CashCountScoreModel.class).equalTo(CashCountScoreModel.isCashOnHand,true).findAll();
cashCountScoreModels.deleteAllFromRealm();
}
});

@Md。 Nowshad Hasan 是正确的。领域线程中只有 运行。

您不应在循环内调用 deleteFromRealm(i),因为它总是会导致崩溃。请改用此代码:

    realms.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            RealmResults<CashCountScoreModel> results = CashCountScoreModel.getAll(realm);
            results.where().equalTo("isCashOnHand", true).findAll().deleteAllFromRealm();
        }
    });