GreenDao asynchronously loadAll methood

不羁岁月 提交于 2019-12-23 02:35:00

问题


I can successfully insert rows asynchronously using GreeDAO's AsyncSession like this:

getMyObjectDao().getSession().startAsyncSession().insertOrReplaceInTx(MyObject.class, list);

How can I load all objects from db into ArrayList asynchronously. So far I have tried below code but its not working:

1-

<List>items = getBoxDao(c).getSession().startAsyncSession().loadAll(MyObject.class);

2-

        @Override
        public void onAsyncOperationCompleted(AsyncOperation operation) {
            String operationIs = null;

            switch (operation.getType()) {

                case LoadAll:
            itemsList = BoxRepository.getAllBoxes(getApplicationContext());

回答1:


You can grab the fetched arrayList with operation.getResult() method, like this:

AsyncSession asyncSession = App.getInstance().daoSession.startAsyncSession();
    asyncSession.setListener(new AsyncOperationListener() {
        @Override
        public void onAsyncOperationCompleted(AsyncOperation operation) {
            itemsList = (List<MyObject>) operation.getResult();
        }
    });
    asyncSession.loadAll(MyObject.class);

UPDATE: BoxRepository is just a Helper class, its getAllBoxes() loadsAll data synchronously but you can easily add another method like this which loads all the data asynchronously:

public static void getAllBoxes(Context context, AsyncOperationListener listener) {
    AsyncSession asyncSession = App.getInstance().daoSession.startAsyncSession();
    asyncSession.setListener(listener);
    asyncSession.loadAll(Box.class);
}


来源:https://stackoverflow.com/questions/30121550/greendao-asynchronously-loadall-methood

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!