0

I'm getting records from web service using a retrofit. Getting records does not take much time but inserting in local db takes a few minutes. I tried inserting it using batch operation but I did not observe a big difference. Below is my web service response and db insertion method. Kindly check where I'm making mistake.

 private void getResponseAndInsert(){

    progressDialog = createProgressDialog(getActivity(), false);
    progressDialog.show();
    final ContentServiceCall request = ServiceGenerator.createService(ContentServiceCall.class, "Empty");
    final Call<VODServiceResponse> call = request.getAllVODCategories(Constants.WS_VERSION,Constants.LOCAL_EN,Constants.PLATFORM);
    call.enqueue(new Callback<VODServiceResponse>() {
        @Override
        public void onResponse(Call<VODServiceResponse> call, final Response<VODServiceResponse> response) {

            if(response!=null && response.isSuccessful())
            {
                if(response.body()!=null && response.body().getResponse()!=null)
                {
                    if(response.body().getResponse().getResponseCode()== Constants.RESPONSE_CODE_SUCCESS)
                    {
                        if(response.body().getVODCategories() != null)
                        {
                            VODCategories vodCategories;
                            for (int i = 0; i < response.body().getVODCategories().size(); i++) {
                               //inserting in vod categories
                                vodCategories = response.body().getVODCategories().get(i);
                                dbHelper.insertVODCategories(vodCategories);
                            }
                        }
                        else {
                            // No data returned etc
                        }
                    }
                    else
                    {
                        //response.body().getResponse().getMessage() display in toast
                        //Toast.makeText(getActivity(),response.body().getResponse().getMessage(),Toast.LENGTH_LONG).show();
                    }

                }
                else
                {
                    // leave it
                }
            }
            else
            {
                // Display proper message
            }
            progressDialog.dismiss();
        }
        @Override
        public void onFailure(Call<VODServiceResponse> call, Throwable t) {
            Log.e("Fail", "Failure");
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
            //Toast.makeText(getActivity(),"Please Check your Internet Connection ",Toast.LENGTH_LONG).show();

        }
    });
}

db insertion method

    public boolean insertVODCategories(VODCategories vd) {

    db = this.getWritableDatabase();
    db.beginTransaction();
    try {
        ContentValues initialValues = new ContentValues();
        initialValues.put(VODCATEGORIES_COLUMN_VODPARENTCATGORYID, vd.getVODParentCategoryId());
        initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYID, vd.getVODCategoryId());
        initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYNAME, vd.getVODCategoryName());
        initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYTHUMBNAILPATH, vd.getVODCategoryThumbnailPath());
        initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYIMAGEPATH, vd.getVODCategoryImagePath());
        initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYIMAGEPATHLARGE, vd.getVODCategoryImagePathLarge());
        initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYDESCRIPTION, vd.getVODCategoryDescription());
        initialValues.put(VODCATEGORIES_COLUMN_VODADDEDDATE, vd.getVODCategoryAddedDate());

        db.insert(TABLE_VODCATEGORIES, null, initialValues);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        return true;

    }
}

1 Answer 1

2

You are using the batch operation wrong. You are doing a single insert in a db transaction which makes the transaction useless. The point of a transaction is to do multiple operations.

You could do this:

List<VODCategories categories = response.body().getVODCategories();
List<ContentValues> valuesList = new ArrayList<>();
VODCategories vd;
for (int i = 0; i < categories.size(); i++) {
   vd = categories.get(i);
   ContentValues initialValues = new ContentValues();
    initialValues.put(VODCATEGORIES_COLUMN_VODPARENTCATGORYID, vd.getVODParentCategoryId());
    initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYID, vd.getVODCategoryId());
    initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYNAME, vd.getVODCategoryName());
    initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYTHUMBNAILPATH, vd.getVODCategoryThumbnailPath());
    initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYIMAGEPATH, vd.getVODCategoryImagePath());
    initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYIMAGEPATHLARGE, vd.getVODCategoryImagePathLarge());
    initialValues.put(VODCATEGORIES_COLUMN_VODCATEGORYDESCRIPTION, vd.getVODCategoryDescription());
    initialValues.put(VODCATEGORIES_COLUMN_VODADDEDDATE, vd.getVODCategoryAddedDate());
    valuesList.add(initialValues);
}
dbHelper.insertVODCategories(valuesList);

Create a list of ContentValues for all objects and then

public boolean insertVODCategories(List<ContentValues> values) {

    db = this.getWritableDatabase();
    db.beginTransaction();
    try {
        for(int i = 0; i < values.size(); i++) {
            db.insert(TABLE_VODCATEGORIES, null, values.get(i));
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        return true;

    }
}

insert all those values in a single transaction which should be faster.

Also onResponse is executed on the main thread. You might want to do the db operations on a background thread so you don't block the UI.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.