Sqlite операция зависает пользовательский интерфейс на экранах Android - PullRequest
0 голосов
/ 24 августа 2018

Я вызываю API внутри службы намерений и сохраняю данные из ответа API в локальной базе данных sqlite. Предположим, что в базе данных есть 1000 записей.намеренное действие службы находится в фоновом режиме без загрузчика.Теперь во время этой операции я пытаюсь переместить другие экраны, приложение зависает до тех пор, пока все записи не будут сохранены в локальной базе данных, когда все 1000 записей загружены и сохранены в sqlite. Я могу переместить их в любое место, где приложение не зависает.Но я хочу переместить дополнительные экраны, когда 5 записей сохранены, приложение зависает в этом случае.Пожалуйста, помогите и дайте мне знать, что я делаю неправильно.Это мой сервисный код намерения -

 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();
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...