0

On api level 19 app is giving this exception while counting junk files... I know it's some iteration problem ,but how to get over it

    protected Void doInBackground(Void... params)
{
    m_oNotificationManager =(NotificationManager)oContext.getSystemService(Context.NOTIFICATION_SERVICE);
    m_o_globalClass = (GlobalClass) oContext.getApplicationContext();
    StorageDetect.prepareStorageList(oContext);
    if(m_o_globalClass != null)
    {
        m_o_globalClass.getJunkManager().initReclaimable((byte) (JunkManager.m_kEmpty | JunkManager.m_kLog | JunkManager.m_kTemp));
        for (Storage obj : m_o_globalClass.getStorageList())
        {
            // sz_storage contain the path(mountpoint) of the the storage.
            String sz_storage = obj.m_sz_path;
            File o_file = new File(sz_storage);
            //here condition checking for usb.if mount point will contain the below keyword than it will not find the file for this mount point.
            if (o_file.toString().toLowerCase().contains("usb") || o_file.toString().toLowerCase().contains("usbdisk") || o_file.toString().toLowerCase().contains("usbcard") || o_file.toString().equals(Environment.getDataDirectory().getPath()))
            {
                continue;
            }
            getEmptyFolder(o_file);
            getLogFile(o_file);
            getTempFile(o_file);
        }
    }
    return null;
}
sam
  • 113
  • 1
  • 9
  • *but how to get over it* ... first you need to read stacktrace to understand where the exception occurs ... I bet that not in this code ... even more ... I bet on `getStorageList()` – Selvin Nov 09 '16 at 11:29
  • It's unclear what's inside `GlobalClass` and what `getStorageList()` returns, but obviously the list is modified while it's also being iterated. The "silly" option is to make a copy of the list and iterate over the copy. The smarter option (if suitable in this case) is to change the type of that `List` to thread-safe one. There are for example options like [synchronizedList](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedList(java.util.List)) and [CopyOnWriteArrayList](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html). – Markus Kauppinen Nov 09 '16 at 11:42
  • Maybe if you shed some light on `GlobalClass` and `getStorageList()` then somebody can give a more educated answer that considers the caveats and drawbacks of the different options. (Like the need of synchronised blocks and the potential pitfalls there, performance impact on writing vs. iterating and whatnot...) – Markus Kauppinen Nov 09 '16 at 11:44

0 Answers0