Amt87
Amt87

Reputation: 5607

Android Realm delete list of objects misunderstanding

I am having list of objects like the following:

{A:
  {B:"", 
   C:"", 
   D:[{Z:""},
      {Z:[{x:""}]},
      {Z:[{x:""},{x:""},{x:""}]},
      {Z:[{x:""},{x:""}]},
      {Z:[{x:""},{x:""},{x:""},{x:""}]}
    ]
  }
}

I update Z's objects at run time, and at a specific location I want to remove all Z's objects and delete its data. I used the following:

            RealmResults<A> aToEdit = realm.where(A.class).findAll();
            RealmList<D> dsToEdit = aToEdit.get(0).getDs();
            for (int i = dsToEdit.size()-1; i >= 0; i--) {
                D d = dsToEdit.get(i);
                RealmList<Z> z = d.getZ();
                z.deleteAllFromRealm();
            }

AND

            RealmResults<Z> resultToDelete = realm.where(Z.class).findAll();
            resultToDelete.deleteAllFromRealm();

AND

            for (int x = 0 ; x < resultToDelete.size() ; x++){
                resultToDelete.get(x).deleteFromRealm();
            }

AND

            realm.delete(Z.class);

But unfortunately non of them worked fine when I call getZObjects again, it will return the updated objects not empty or null.

Any one can help to find what am I missing here? The goal is to remove all Z's from Realm database.

Upvotes: 1

Views: 845

Answers (2)

beeender
beeender

Reputation: 3565

All of you approaches should just work, and realm.delete(Z.class); should be the easiest way.

There are two possibilities that it doesn't work as far as I can tell:

  1. You might run into some code which saves the Z to the Realm db again, try to set a breakpoint to check?

  2. The code where you check the getZObjects() is not on a looper thread, the Realm db won't get updated automatically there. Try to close/reopen the Realm on the non-looper thread. If it is on a looper thread, the db will get updated in the next even loop.

Upvotes: 2

Sajidh Zahir
Sajidh Zahir

Reputation: 526

if its a known number and a small amounts of cards above approaches would work but if you are trying to generate unknown number of cards its better to use a recycler view

Hope this example helps you https://dzone.com/articles/be-lazy-productive-android-2

Upvotes: 1

Related Questions