Andiana
Andiana

Reputation: 1952

GreenDAO does not update the entity's children tree after insert child entity

I have two GreenDAO entity, "Card" have n-1 relation with "Lesson".

public class Card {

    private Long id;
    private String SourceText;
    private String TargetText;
    private byte[] Image;
    private Long Point;
    private Long lessonID;
}

public class Lesson {

    private Long id;
    private String LessonName;
    private String ShortDes;
    private String LongDes;
    private byte[] Picture;
    private java.util.Date CreatedDate;
    private String CreatedBy;
    private Long sourceLangID;
    private Long targetLangID;

    /** Used to resolve relations */
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    private transient LessonDao myDao;


    private List<Card> cards;
}

In activity A1 - viewing a lesson L1, I used startActivityForResult(A2), go to A2, and insert some cards to L1's cards list. The record is inserted to DB, but when I finish() in A2 and back to A1, in the onResult event:

void loadCards() {
        daoSession = null;
        daoSession = ((FlashcardApplication) getApplicationContext()).daoSession;
        lessonDao = daoSession.getLessonDao();
        currentLesson = null;
        currentLesson = lessonDao.loadByRowId(id);
        cards = currentLesson.getCards();
        lblLessonName.setText("Cards of "+currentLesson.getLessonName());
         daoSession = ((FlashcardApplication) getApplicationContext()).daoSession;
       // cards = cardDao.queryDeep("lessonID = " + currentLesson.getId(), null);
        CardListAdapter adapter = new CardListAdapter(getApplicationContext(), R.layout.lv_item_card_of_lesson, cards);
        lvCards.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1){
            if(resultCode == CardCreateActivity.SUCCESS){
                loadCards();
                Toast.makeText(getApplicationContext(), "This set have " + cards.size() +" cards",Toast.LENGTH_SHORT).show();
            }
            return;
        }
        Toast.makeText(getApplicationContext(),"Unknowed intent when back to LocalLessonCardManage",Toast.LENGTH_SHORT);
    }

The cards list is not updated. It still be the same like before inserting. When I relaunch and visit activity A1, the new records displayed. I think GreenDAO is keeping the state of the lesson object L1. I know lessonDao.refresh(l1) is update only L1 but not its children tree. Is there a way to refresh the cards list? I want to keep using startActivityForResult(), may a dirty-way startActivity() to go from A2 to A1 will work but I don't like it.

Upvotes: 1

Views: 775

Answers (1)

Jofre Mateu
Jofre Mateu

Reputation: 2430

For performance reasons, the relations are cached and must be updated manually when a modification is made.

Try with this

currentLesson.resetCards();
cards = currentLesson.getCards();

Here you have more info about this

Upvotes: 4

Related Questions