Mark Swedish Mark Swedish - 3 months ago 136
Java Question

java.lang.IllegalStateException whenever I try to add values into a populated RealmList<MyObject>

I'm trying to add new items insedi a RealmList but I'm getting this error :

java.lang.IllegalStateException:
Changing Realm data can only be done from inside a transaction.


Do you know why it is happening?

Thanks in advance!

Answer

You should be inside a Realm Transaction to add a new item in this list. The fllowing code si an example how to do it:

    realm.beginTransaction();
    RealmQuery query = realm.where(YourClass.class);
    RealmResults results = query.findAll();
    results.add(newItem);
    realm.commitTransaction();

I hope it can be helpful.

Comments