0

It is throwing an error:

Caused by: java.lang.IllegalArgumentException: You must call this method on the main thread

Code:

Class CacheClearAsyncTask extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {
        Glide.get(getActivity()).clearDiskCache();
        Glide.get(getActivity()).clearMemory();

        return null;
    }

    @Override
    protected void onPostExecute (Void result)
    {
        //Toast.makeText(getActivity(), "Cache cleared", Toast.LENGTH_LONG).show();
    }
}

Setting preference on click event:

clearCacheBtnPref=findPreference(getResources().getString(R.string.pref_btn_clear_cache_key));
    clearCacheBtnPref.setOnPreferenceClickListener(new 

Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    new CacheClearAsyncTask().execute();
                    return true;
                }
            });

This can not be called in main thread as it is also throws errors and also it does not let me use asynctask.

    Glide.get(getActivity()).clearDiskCache();
    Glide.get(getActivity()).clearMemory();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
varuog
  • 3,031
  • 5
  • 28
  • 56

3 Answers3

1

clearDiskCache() must be called from background thread while clearMemory() from main thread so:

class CacheClearAsyncTask extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {
        Glide.get(getActivity()).clearDiskCache();    
        return null;
    }

    @Override
    protected void onPostExecute (Void result)    {
        Glide.get(getActivity()).clearMemory();
    }
}
lelloman
  • 13,883
  • 5
  • 63
  • 85
0

I don't know your use case or app flow. But doInBackground is specifically used to do operations in background thread than Main UI thread.

Now if you need to do changes corresponding to Main UI thread in doInBackground , then do as following

@Override
    protected Void doInBackground(Void... params) {
        getActivity().runOnUiThread(new Runnable() {
                       @Override
                       public void run() {

                            Glide.get(getActivity()).clearDiskCache();
                            Glide.get(getActivity()).clearMemory();
                       }
                   });

        return null;
    }

For more examples on runOnUiThread refer this

Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • No, you should put it in onPostExecute or onPreExecute – Thanh Le May 11 '17 at 07:30
  • Yah I agree. onPostExecute is specifically for performing in UI thread. But what if this guy needs to perform something in a loop basis and on doInbackground which will update the UI thread? That is the first sentence of my answer... that I dont know his use case or app flow. But if he really wants to update UI in doInBackground, then the solution will work – Sreehari May 11 '17 at 07:32
0

Try this

Both method works in different thread.

clearDiskCache() must be called on a background thread.
clearMemory() must be called on the main thread.
You can't call both at once on the same thread.

void clearGlideCache()
{
    new Thread() 
    {
        @Override
        public void run() 
        {
            Glide.get(DemoActivity.this).clearDiskCache();
        }
    }.start();

    Glide.get(DemoActivity.this).clearMemory();
}