daniel
daniel

Reputation: 461

Updating parent entity when child entity is added (Greendao)

I am using greendao in my android app and an Activity that needs to refresh some item in a listview.

I have an entity "activity" that has an one-to-many relation to "exports"

In order to update the "activity" when a new "exports" is added I do as described in http://greenrobot.org/greendao/documentation/relations/#Resolving_and_Updating_To-Many_Relations

Exports ex = new Exports();
ex.setActivityId(activityEntry.getId());
//DOING something...
exportsDao.insert(ex);
List<Exports> exportList = activityEntry.getExportsList();
exportList.add(ex);

This however gives me an java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:404)

What am I doing wrong?

Upvotes: 2

Views: 329

Answers (2)

jrhee17
jrhee17

Reputation: 1152

Just came across this same issue -- You probably have this fixed by now but this is for reference for any recent users (as of Apr.25 2016)

The bugfix was fixed recently (early this year) as shown here

https://github.com/greenrobot/greenDAO/issues/287 https://github.com/greenrobot/greenDAO/pull/288

Judging from the above, if anyone has the same issue, the problem is most likely one of the following

  1. You don't have the most recent greendao dependency version (which was prior to the recent fix)
  2. Or you were like me and pulled the greendao project from github and mindlessly copied the gradle dependency version from the README file -- which looks like the following

    compile 'de.greenrobot:greendao:2.1.0'
    

    which looks different from the gradle dependency on the official website.

    compile 'org.greenrobot:greendao:2.2.0' 
    

Upvotes: 3

tcarvin
tcarvin

Reputation: 10855

From the page you referenced, you have done the steps out of order. Follow the steps in this order and see if it addresses the problem.

This is how to insert new entities that are part of a to-many relation:

  1. Get the to-many Java List (This has to be done this before persisting the new entity, because we do not know if we get a cached for fresh result. Like this, we know it’s cached now.)
  2. Create a new entity object (on the many side)
  3. Set the foreign property of the new entity to the target entity
  4. Persist the new object using insert
  5. Add the new object to the to-many Java List

Upvotes: 0

Related Questions