插入子实体后,GreenDAO不会更新实体的子树

时间:2016-04-14 01:15:44

标签: android orm refresh updates greendao

我有两个GreenDAO实体," Card"与"课程"。

有n-1关系
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;
}

在活动A1中 - 查看课程L1,我使用了startActivityForResult(A2),转到A2,并将一些卡片插入到L1的卡片列表中。记录被插入到DB中,但是当我在A2中完成()并返回到A1时,在onResult事件中:

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);
    }

卡片列表未更新。插入前它仍然是一样的。当我重新启动并访问活动A1时,会显示新记录。我认为GreenDAO保持课程对象L1的状态。我知道lessonDao.refresh(l1)仅更新L1,但不更新其子树。有没有办法刷新卡片清单?我想继续使用startActivityForResult(),从startActivity()A2的肮脏方式A1可行,但我不喜欢它。

1 个答案:

答案 0 :(得分:4)

出于性能原因,关系会被缓存,并且必须在进行修改时手动更新。

试试这个

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

Here您有关于此

的更多信息