How to know if an image is being cached on Watch

In WatchKit, Apple gives us about 20 MB of image cache space.

This cache persists across all runs, and images crash when the run ends.

The problem is this: how do you know if an image is in the cache or not?

Consider this scenario: you cache an image to a watch with a key for future display. When it comes time to display an image, how do you know it's still being cached?

If there is no way to find out, you should cache it again. This will completely destroy the purpose of the cache if you have to send it to the device every time you show it.

Edit . The API has been updated to fix this in the iOS 8.2 beta. See Dave's doc for details.

Edit2 . This was completely fixed in the iOS 8.2 beta. See John's answer for details.

+3


source to share


2 answers


In addition to Dave's answer, it's worth noting that it also has a WKInterfaceDevice property cachedImages

that returns an NSDictionary of all cached images on the current device for the running application.

From WKInterfaceDevice Class Reference :



Each entry in the dictionary is an NSString containing the name associated with the image. The value of each record is an NSNumber object containing the size of the image, in bytes. Whenever you need to remove images from your cache, use this information to help you choose which images to remove.

+4


source


This functionality is achievable using the iOS 8.2 beta. In B2, methods -addCachedImage:named:

now return BOOL

. It will return YES

if the image was added to the cache and NO

if it wasn't (since the size limit has been reached).

The cache will also not be cleared by the OS; you need to clear it yourself using the appropriate method -remove...

.



This way, if your extension maintains a list of all the images it has ever sent to Watch, you can finally know which images are in the cache.

+1


source







All Articles