I'm currently trying to delete specific realm object in my model using for loop,

but every time that i execute the deleteFromRealm(i) it stops the loop and I can no longer delete the other object.

I haven't tried any other options though.

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

Whenever i try to run the app, and execute this specific code cashCountScoreModels.deleteFromRealm(i);, it stops the loop.

4 Answers

0
Anggrayudi H On Best Solutions

You should not call deleteFromRealm(i) inside a loop because it always causes crash. Use this code instead:

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

Not sure what version of Realm you are using. But since 3.0.0, Realm collections are live and hence updated immediately. So, cashCountScoreModels.size() will return a count less upon every deletion. In your case, I suspect you have just 2 entries in the collection. You might want to use OrderedRealmCollectionSnapshot instead. Try out following code.

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

Look for Iterations & snapshots on 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 to know more realtime update in collections and OrderedRealmCollectionSnapshot

0
Md. Nowshad Hasan On

It happens because I think you want to delete multiple realm objects from a single execution block. Try below code in execute block.

RealmResults<CashCountScoreModel> cashCountScoreModels=realm.where(CashCountScoreModel.class).equalTo(CashCountScoreModel.isCashOnHand,true).findAll();
cashCountScoreModels.deleteAllFromRealm();
0
kashyap On
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 is Correct. Just run in Realm Thread.