0

I am trying to pull data from my webserver and add them to Local DB in background using a service.. Data is getting stored to Local DB without any issues but while this is happening the phone becomes slow (there is some dragging). Just wondering if I am doing things correctly in writing to DB or is there better and faster way to do it.

I am using AsyncHttpClient library to pull data from webserver.

final RequestParams requestParams = new RequestParams();
        final String uploadWebsite = url_acc_details;

        AsyncHttpClient client = new AsyncHttpClient();
        client.post(uploadWebsite, requestParams, new JsonHttpResponseHandler() 
        {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) 
            {
                try 
                {
                    success = response.getInt(TAG_SUCCESS);

                    if (success == Integer.parseInt(getResources().getString(R.string.successvalue)))
                    {
                        details = response.getJSONArray(TAG_DETAIL);
                        for (int i = 0; i < details.length(); i++) 
                        {
                            JSONObject c = details.getJSONObject(i);

                            acc_iD = c.getString(TAG_ACC_ID);
                            acc_nAme = c.getString(TAG_ACC_NAME);
                            acc_Image = c.getString(TAG_ACC_IMAGE);
                            acc_active = c.getString(TAG_ACC_ACTIVE);
                            acc_priority = c.getString(TAG_ACC_PRIORITY);
                            acc_creator = c.getString(TAG_ACC_CREATOR);

                            dblocal.setAccDetails(Integer.valueOf(acc_iD), acc_nAme, acc_Image, acc_active, acc_priority, acc_creator);
                        }
                    }
                } 
                catch (JSONException e) 
                {
                    e.printStackTrace();
                }
                stopSelf();

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) 
            {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                stopSelf();
            }
        });     

Similarly I am starting three services running in bankground at the same time with same above logic but for getting different details.

It would be great if somebody could let me know the way to do the above process faster and better?

user5287166
  • 223
  • 1
  • 2
  • 15

1 Answers1

0

First, you should use Singleton pattern for your AsyncHttpClient. This way, you don't have to redefine a new resource for each and every request.

public class MyAsyncHttpClient extends AsyncHttpClient {
    private static MyAsyncHttpClient client;

    private MyAsyncHttpClient() {

    }

    public static MyAsyncHttpClient getClient() {
        if (client == null) {
            client = new MyAsyncHttpClient();
            // do whatever, setCookieStore, addHeader, etc
        }      


        return client;
    }
}

Then in your asyncTask

replace new AsyncHttpClient()

with MyAsyncHttpClient.getClient()

Second, do your database operation in background as well. Declare an interface:

public interface DatabaseOperationListener {
    public void perform();
    public void onFinish();
}

...


client.post(uploadWebsite, requestParams, new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) 
    {
        new DatabaseTask(new DatabaseOperationListener() {
            @Override
            public void perform() {
                DatabaseManager dblocal = DatabaseManager.getInstance();
                dblocal.setAccDetails(Integer.valueOf(acc_iD), acc_nAme, acc_Image, acc_active, acc_priority, acc_creator);

            }

            @Override
            public void onFinish() {
                // do something, perform another request, etc
            }
        }).execute();

   @Override
   public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable error) {
       super.onFailure(statusCode, headers, errorResponse, error);
       // do something
   }
});

Do note that I also have a Singleton instance for the database.

Fadils
  • 1,508
  • 16
  • 21
  • Thanks for the quick reply. Just wondering by doing this way will I be able to improve the processing time and do you think the dragging will not be there? – user5287166 Sep 18 '15 at 03:06
  • One thing for sure that by doing this, then it will greatly improve the performance. – Fadils Sep 18 '15 at 03:10
  • From the code you gave us, the code is fine. So, this is an improvement that I can think of. If after doing this improvement, the drag is still there, then you might want to take a look at other causes. – Fadils Sep 18 '15 at 03:21
  • Is there an example to do this? – user5287166 Sep 22 '15 at 08:03
  • @user5287166 I thought I already gave you an example in above. Have you tried it? – Fadils Sep 22 '15 at 08:55
  • I have completed the singleton for AsyncHttpClient it is working fine but I couldn't understand the DB part. – user5287166 Sep 22 '15 at 09:52
  • Basically, to improve your database operation, you have to move `dblocal.setAccDetails` into a background task as well. This way, your main thread doesn't have to wait your db write/read to finish (which might be the cause for "draggin"). If you find my answer helpful, could you please mark the answer as accepted? – Fadils Sep 22 '15 at 10:06
  • I actually understood that but how do I implement? can you give me an example the above I couldn't understand. – user5287166 Sep 22 '15 at 11:57
  • Ahh, I see. Something for copy-and-paste? – Fadils Sep 22 '15 at 13:04
  • Figured it out myself. Thanks for your help! – user5287166 Sep 26 '15 at 01:19