Optimizing the space in the client's storage w

2019-09-06 07:49发布

问题:

In Codenameone I have the following code:

String url = "http://www.example.com/advertisement.png";
String key = "goose_id-"+System.currentTimeMillis();
Dimension dimension = new Dimension(200, 200);
ImageDownloadService.createImageToStorage(url, label, key, dimension);
// Please note that every image will be assigned to 
// a storage key that begins with "goose_id_" string

In this way, every time I change the file advertisement.png in the server the user, in his device, will get a different image.

My only one concern is: After several changes of the file in the server, the user is going to have ALL those images in his device. Is this correct?

I want to know if this strategy will end up running out of space the users's device.

If this is true, How can I "clean" the storage objects that begins with "goose-id-" ?

回答1:

Yes, everything gets written to storage for caching.

You can prefix the cache key with a "toDelete_" string then use the Storage class to loop over entries and delete them periodically. You can also do this in the stop() method of the main class to clear the storage when the app gets suspended.