Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happened to removing items from realm.io? RealmException "Removing object is not supported."?

I am trying to delete the last Object from the Realm.io database based on a query, like so:

    Realm realm = Realm.getInstance(this);
    final RealmResults<RealmCustomLocation> databaseLocations = realm.where(RealmCustomLocation.class).findAllSorted("timeStamp", RealmResults.SORT_ORDER_DESCENDING);
    if(databaseLocations.size() >= 4){
        realm.beginTransaction();
        databaseLocations.removeLast();
        realm.commitTransaction();
    }

This is exactly like what is written at the Realm.io instructions about deletion:

realm.beginTransaction();
result.removeLast();
realm.commitTransaction()

But when I execute the code it always breaks with a RealmException

io.realm.exceptions.RealmException: Removing object is not supported.

Then I looked at the source code of RealmResults.java and I find this: enter image description here So no wonder it keeps crashing, removeLast() does nothing, only throw an error!

So my question is: How can I remove an object from the database then?!

I am using realm.io 0.77 (compile 'io.realm:realm-android:0.77.0') on Android.

I appreciate your help on this!

like image 493
A. Steenbergen Avatar asked Jan 18 '15 13:01

A. Steenbergen


1 Answers

I have contacted Realm.io support, awaiting an answer. For the meantime:

RealmCustomLocation location = databaseLocations.get(databaseLocations.size() - 1);
location.removeFromRealm();

works equivalent to

databaseLocations.removeLast()

so it can be used as a workaround.

Edit: Support told me that they are fixing it for future versions and recommended to use the workaround I posted for in the mean time.

like image 153
A. Steenbergen Avatar answered Oct 21 '22 05:10

A. Steenbergen