Save robospice request result saved in sqllite freeze UI

141 Views Asked by At

Im using Robospice (best lib so far for me) with Sping ang Gson to communicate with my webservices - simple, clean and working good. Now I want store my data in sql database - eg for offline mode. My problem is how to combine robospice with ormlite or how to use robospice extension for ormlite.

So far Ive created this

public class MyActivity extends Activity {

private void performRequest() {
    MyRequest request = new MyRequest();
    spiceManager.execute(request, request.createCacheKey(),
            DurationInMillis.ONE_MINUTE, new MyRequestListener());
}

private class MyRequestListener implements
        RequestListener<MyDaoList> {
    @Override
    public void onRequestFailure(SpiceException e) {
        ...
    }

    @Override
    public void onRequestSuccess(MyDaoList myDaoList) {

        RuntimeExceptionDao<MyDao, Integer> myDao =  mDatabaseHelper.getMyDataDao();
        for (MyDap mDao : myDaoList) {
            myDao.createIfNotExists(mDao);
        }

        ...
    }
 }
}

works fine, but not perfect. Data is downloaded, and saved in my sql database. But this solution freeze my UI when a lot of data need to be saved. How can I avoid this freeze?

I have checked robospice samples with sqllite but I have problems with samples and so far looks like it is not finished extension.

1

There are 1 best solutions below

0
On

UI freeze because onRequestSuccess method is called on UI thread (and it's perfectly normal) and saving data takes some time. So you should create new thread (or AsyncTask) in onRequestSuccess and save data to DB in this new thread.