Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9414

AsyncTask block UI thread

I have a problem with AsyncTask, it block UI and make screen freeze

this is my code

new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... voids) {

                    AdServerManager.getInstance().getOfferClickCount(MultiOfferDetailsActivity.this, mCurrentOffer.getId(), ClickType.LIKE, new ClickViewCountUIListener() {
                        @Override
                        public void onClickViewCountCompleted(IncrementClickResponse response, AppError error) {
                            if (response != null)
                                mOfferLikes.setText(getString(R.string.likes2) + " " + PhoneUtils.decimalFormat(MultiOfferDetailsActivity.this, response.getCount()));
                            else
                                mOfferLikes.setText("");
                        }
                    });
                    return null;
                }
            }.execute();

and this is the code of server method

public void getOfferClickCount(final Context context, final int id, final ClickType clickType, final ClickViewCountUIListener uiListener) {

        if (BasePhoneUtils.isNetworkAvailable(context) == false) {

            uiListener.onClickViewCountCompleted(null, AppError.INTERNET_ERROR);

        } else {

            new AsyncTask<Void, Void, BaseResponse>() {

                @Override
                protected BaseResponse doInBackground(Void... sparams) {

                    mStartTime = System.nanoTime();

                    try {

                        String urlToCall = AdsConstants.ADS_SERVER_URL + AdsConstants.OFFER_GET_CLICK_COUNT;
                        String param = String.format(AdsConstants.OFFER_GET_CLICK_COUNT_PARAM_FORMAT, id, clickType.getValue());

                        Log.e("AdServerManager", urlToCall);
                        Log.e("AdServerManager", param);

                        BaseRequest request = new BaseRequest();
                        request.setURL(urlToCall);
                        request.setRequestType(RequestType.POST);
                        request.setStringParam(param);

                        mStopTime = System.nanoTime();
                        PerformanceManager.getInstance().showCalculatedTime("getOfferClickCount [doInBackground]", mStartTime, mStopTime);

                        return NetworkManager.execute(context, request, false, true);

                    } catch (Exception e) {
                        e.printStackTrace();
                        uiListener.onClickViewCountCompleted(null, AppError.DATA_ERROR);
                        return null;
                    }
                }

                protected void onPostExecute(BaseResponse result) {

                    mStartTime = System.nanoTime();

                    if (result != null && result.getResponse() != null) {
                        try {
                            uiListener.onClickViewCountCompleted(IncrementClickResponse.parseJSONObject(result.getResponse()), null);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            uiListener.onClickViewCountCompleted(null, AppError.PARSING_ERROR);
                        } catch (ParseException e) {
                            e.printStackTrace();
                            uiListener.onClickViewCountCompleted(null, AppError.PARSING_ERROR);
                        }
                    } else {
                        uiListener.onClickViewCountCompleted(null, AppError.GENERAL_ERROR);
                    }

                    mStopTime = System.nanoTime();
                    PerformanceManager.getInstance().showCalculatedTime("getOfferClickCount [onPostExecute]", mStartTime, mStopTime);
                }
            }.execute();
        }
    }

Upvotes: 1

Views: 1339

Answers (1)

AGDownie
AGDownie

Reputation: 638

You appear to have multiple AsyncTasks active at the same time trying to communicate with each other. As of Honeycomb, the default executor for AsyncTask is the SERIAL_EXECUTOR, so only one AsyncTask can run at any time and you may be suffering a deadlock situation. You could try executing the tasks using the THREAD_POOL_EXECUTOR as a possible quick fix, i.e. instead of .execute() use .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

Upvotes: 2

Related Questions