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

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

我有两个 GreenDAO 实体,"Card" 与 "Lesson" 有 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;
}

在activity A1 - 观看L1课时,我使用了startActivityForResult(A2),转到A2,并将一些卡片插入到L1的卡片列表中。记录被插入到数据库中,但是当我在 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);
    }

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

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

试试这个

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

Here 你有更多关于这个的信息