Rashad.Z
Rashad.Z

Reputation: 2604

Executing Multiple Queries In IntentService

i have an IntentService which performs a webservice call using retrofit, then on success the response (Set of queries) will be executed. the webservice call is being made successfuly and all is well, but when executing the queries, the UI freezes then continues when the execution finishes.

shouldnt an intent service do tasks in background without affecting the UI?

code simplified:

@Override
public void success(Response iu, Response response) {
    //get respose (set of queires then loop over them and execute them.
    for (int i = 0; i < contentArray.length; i++) {
        String query = contentArray[i];
        MainActivity.myDataBase.execSQL(query);
}  //the amount of queries can reach 100

if you would like to post more code i will

Upvotes: 2

Views: 544

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007474

You appear to be making the Web service call using Retrofit, and in particular, using a Retrofit Callback. The Callback is designed for cases were you are initiating the query from the main application thread and you want the results to be delivered to the main application thread (e.g., to update a UI).

In your case, none of that is true.

Instead, drop the Callback and use Retrofit's synchronous API. So, instead of something like:

@GET("/group/{id}/users")
void groupList(@Path("id") int groupId, Callback<List<User>> callback);

use something like:

@GET("/group/{id}/users")
List<User> groupList(@Path("id") int groupId);

This way, the results will be delivered to you on the same thread that you are on, synchronously, and it will ensure that you are on the background thread for your database I/O.

BTW, if you are not doing so already, consider wrapping those database calls in a transaction -- doing ~100 individual transactions may get a little slow.

Upvotes: 6

Related Questions