0

I am calling APIs inside intent service & saving the data from API response in local database sqlite suppose there are 1000 records to be saved in database. intent service action is in background without loader. Now during this operation I am trying move other screens, app hangs until all records are saved in local database , when all 1000 records are downloaded & saved in sqlite I can move anywhere application does not hang. But I want to move further screens when 5 records are saved,app hangs in that case. Please help & let me know what I am doing wrong. This is my intent service code--

 protected void onHandleIntent(Intent intent) {
        try {
                downloadStores();
            } catch (Exception e) {
                e.printStackTrace();
            }
}
   public void downloadStores()
{
    ArrayList<DownloadStoreItem> targetsTobeDownloaded=new ArrayList<DownloadStoreItem>();
    targetsTobeDownloaded=AppUtilityFunction.getTargetstobeDownloaded(dbHelper);
    for (int i=0;i<targetsTobeDownloaded.size();i++)
    {
        DownloadStoreItem downloadStoreItem=targetsTobeDownloaded.get(i);
        downloadStoreInfoService(targetsTobeDownloaded.get(i).getStoreId(), null , 4,downloadStoreItem);


    }
}



    public void downloadStoreInfoService(final String storeId, final String 
 storeVersion, final int storeType, final DownloadStoreItem downloadStoreItem) {
        try {

            final VolleyTaskListener taskListener = new VolleyTaskListener() {
                @Override
                public void postExecute(JSONObject response) {
                    Log.d("StoreInfo Response", response.toString());
                    StoreInfoResponse storeInfoResponse = StoreInfoResponse.create(response.toString());
                    //save in local DB
                    StoreInfoItem storeInfoItem = new StoreInfoItem(storeInfoResponse.getVersion(), storeInfoResponse.getStore_id(), storeInfoResponse.getStoreName(), storeInfoResponse.getClassificationid(), storeInfoResponse.getClassification(), storeInfoResponse.getAddress(), storeInfoResponse.getCityID(), storeInfoResponse.getCity(), storeInfoResponse.getLatitude(), storeInfoResponse.getLongitude(), storeInfoResponse.getStatus(), storeInfoResponse.getRoleType(), storeType);
                   StoreInfoDao storeInfoDao=new StoreInfoDaoImpl();
                   StoreInfoItem oldStoreItem=storeInfoDao.getStoreInfoByStoreId(storeInfoItem.getStoreId(),dbHelper);
                 if(oldStoreItem==null) {
                     long saved = saveStoreInfoLocal(storeInfoItem);
                     if (saved > 0) {
                         Log.d("Store Info saved", "yes");
                         downloadCategoryService(storeInfoItem, storeType);
                     }
                 }
                 else
                 {
                     if(oldStoreItem.getVersion().equalsIgnoreCase(storeInfoItem.getVersion()))
                     {
                         Log.d("No need to download","yes");
                         String started=ProjectPrefrence.getSharedPrefrenceData(AppConstants.PROJECT_PREF_DOMAIN,"started",getApplicationContext());
                    if(started!=null) {
                        if (started.equals("0")) {
                            if (getCategories(storeInfoItem.getStoreId()) == null) {
                                downloadCategoryService(storeInfoItem, storeType);
                            } else {
                                if (getformData(storeInfoItem.getStoreId()) == null) {
                                    downloadFormData(storeInfoItem, storeType);

                                } else {
                                    if (getAllFormdetails(storeInfoItem.getStoreId()) == null) {
                                        downloadFormData(storeInfoItem, storeType);
                                    } else {
                                        _downloadnotiCount++;

                                    }
                                }
                            }

                        }
                    }
                         //_downloadnotiCount++;

                     }
                     else
                     {
                         long saved = saveStoreInfoLocal(storeInfoItem);
                         if (saved > 0) {
                             Log.d("Store update will occur", "yes");
                             downloadCategoryService(storeInfoItem, storeType);
                         }
                     }
                 }
                    //now download store categories service
if(storeVersion==null)
{
    downloadStoreItem.setStatus(0);
    AppUtilityFunction.updateStatusOfDOwnloadStores(downloadStoreItem,downloadStoreItem.getId(),dbHelper);
}
                }

                @SuppressLint("NewApi")
                @Override
                public void onError(VolleyError error) {
                    if (error != null) {

                        if (error instanceof TimeoutError) {
                            Toast.makeText(getApplicationContext(), com.ppms.ui.R.string.timeout_issue, Toast.LENGTH_LONG).show();

                        } else if (AppUtilityFunction.isNetworkProblem(error)) {
                            Toast.makeText(getApplicationContext(), com.ppms.ui.R.string.internet_connection_message, Toast.LENGTH_LONG).show();
                        } else {
                            com.android.volley.NetworkResponse response = error.networkResponse;
                            if (response != null && response.statusCode != 0) {
                                Log.d("StoreInfo Err Response", response.toString());

                                if (response.statusCode == 300) {

                                } else if (response.statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
                                    Toast.makeText(getApplicationContext(), com.ppms.ui.R.string.server_issue, Toast.LENGTH_LONG).show();

                                }
                                else
                                {
                                    String inavalid ="Target ID  "+storeId+ " is invalid. Please enter a valid Target Id and try again.";

                                    AppUtilityFunction.showNotification(getApplicationContext(),inavalid,
                                            AppConstants.DOWNLOAD_FAILUREINVALID_ID, storeId,null);
                                    AppUtilityFunction.saveInActivityLog(userNm,inavalid,dbHelper );
                                    if(storeVersion==null)
                                    {
                                        downloadStoreItem.setStatus(0);
                                        AppUtilityFunction.updateStatusOfDOwnloadStores(downloadStoreItem,downloadStoreItem.getId(),dbHelper);
                                    }
                                }
                            } else {
                                Toast.makeText(getApplicationContext(), com.ppms.ui.R.string.server_null_issue, Toast.LENGTH_LONG).show();

                            }
                        }
                    }


                }
            };
            String completeUrl =  AppUtilityFunction.getBaseUrl(getApplicationContext())+AppConstants.STORE_INFO_URL + storeId + AppConstants.STORE_INFO1_URL + projectNm + AppConstants.STORE_INFO2_URL + userNm + AppConstants.STORE_INFO3_URL + versionNo + AppConstants.STORE_INFO4_URL + userNm + AppConstants.STORE_INFO5_URL + accType + AppConstants.STORE_INFO6_URL + storeVersion;
            Log.d("completeUrl", completeUrl);
            CustomVolleyGet volley = new CustomVolleyGet(taskListener, completeUrl, null, null, getApplicationContext());

            volley.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
priyanka
  • 171
  • 2
  • 12

0 Answers0