vendredi 6 mai 2016

Understanding content providers concurrency or java.util.concurrent.TimeoutException

I have a simple app. It loads data from server each several minutes, stores it to SQLiteDB and shows in ListView. The network request done in IntentService using Volley lib:

RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(API_CALL, null, future, future);
Volley.newRequestQueue(this).add(jsObjRequest);
try {
    JSONObject response = future.get(NETWORK_TIMEOUT, TimeUnit.SECONDS); //EXCEPTION
    }

For displaying the data I've choose to create custom ContentProvider while ListFragment implements LoaderManager.LoaderCallbacks<Cursor>. This pattern initiates loader at start. Than I was thinking to myself what will happen if at some point of time the user will open the application just in moment when IntentService writes to database, so I decided to try to produce this, I'm starting my IntentService at start along with initLoader() and after about 20 restarts - voila:

java.util.concurrent.TimeoutException 
at com.android.volley.toolbox.RequestFuture.doGet(RequestFuture.java:121)

I was shure (after a lot of reading of amazing blog by Alex Lockwood that SQLite DataBase handles concurrency for me, so may it be a Volley issue? The exception never thrown if I do only write operation, but can be easily reproduced when I try to read and write from DB at same time from different worker threas. Can someone explain to me the issue or provide me with a literature to read?

Aucun commentaire:

Enregistrer un commentaire