0

I am having an issue with StrictMode i have the following AsyncTask

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

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

        pruneRecursive(DiskCache);

        return null;
    }

    void pruneRecursive(File fileOrDirectory){
        if (fileOrDirectory.isDirectory()) {
            for (File child : fileOrDirectory.listFiles()) {
                pruneRecursive(child);
            }
        }else {
            if(checkForPruningAsync(fileOrDirectory)){
                fileOrDirectory.delete();
            }
        }
    }

    public boolean checkForPruningAsync(File file){
        String fileName = file.getName();
        String type = fileName.substring(fileName.lastIndexOf(".")+1);

        Date lastModified = new Date(file.lastModified());

        if(type.equals("json")){
            Date cacheLifetime = new Date(new Date().getTime() - keepJsonFor);
            if(lastModified.before(cacheLifetime)){
                return true;
            }
        }else if(type.equals("txt")) {
            Date cacheLifetime = new Date(new Date().getTime() - keepTxtFor);
            if(lastModified.before(cacheLifetime)){
                return true;
            }
        }else{
            Date cacheLifetime = new Date(new Date().getTime() - keepImagesFor);
            if(lastModified.before(cacheLifetime)){
                return true;
            }
        }

        return false;
    }
} 

It is throwing a StrictMode error (does not seem to be crashing the program but i don't like it popping up)

 android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk

My understanding is that if you put something into an Async Task it meets the requirements of StrictMode. But in this case i seem to be wrong. can someone tell me what I am doing wrong please?

EDIT

Here is how i call the Async Task as requested

public void pruneDiskCache(){
    synchronized (mDiskCacheLock) {
        // Wait while disk cache is started from background thread
        while (mDiskCacheStarting) {
            try {
                mDiskCacheLock.wait();
            } catch (InterruptedException e) {
            }
        }
        if(DiskCache.exists()) {
            new pruneDiskCacheTask().execute();
        }
    }
}

This is called from within my Cache Class which is created as so

mActivity = this;
cacheHandeler = new Cache(mActivity);

from within my main thread.

Alex Mason
  • 21
  • 3
  • Can you include where you call the AsyncTask? – ianhanniballake Mar 06 '15 at 05:00
  • Done a little more research into strict mode. Seems to be a debugging tool to make sure your program runs as fast as possible. So the question is: Is there a better (less resource hungry) way to do this? Since this only happens when the user forces a reload on the data set im going to tag it as a necessary evil. – Alex Mason Mar 06 '15 at 05:13
  • StrictMode only triggers when you are doing the operation on the main thread (which, of course, you shouldn't do) - again, we need to see where you are calling the AsyncTask to see why it is occuring. – ianhanniballake Mar 06 '15 at 05:14
  • added Where it is called. Thanks for being a quick reply :) – Alex Mason Mar 06 '15 at 05:16

2 Answers2

1

Should new pruneDiskCacheTask(); not be "new pruneDiskCacheTask().execute()"?

Shirish Hirekodi
  • 392
  • 4
  • 13
  • nope. Still happening. Well yes it should be. but adding it did not stop the violation – Alex Mason Mar 06 '15 at 05:24
  • Too bad... If I were you, 1. I'd post the logcat to SO. 2. Comment out the methods inside of doInBackground() and progressively add the bits to find out the offending line of code. The problem could be outside of this AsyncTask – Shirish Hirekodi Mar 06 '15 at 05:32
0

Thanks to Shirish Hirekodi i have found the answer. it wasn't the method itself it was the

 if(DiskCache.exists()){

 }

comparison when i called was creating the task that needed to be be put inside the async task.

Thanks.

Alex Mason
  • 21
  • 3