问题
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