3
votes

In WatchKit Apple gives us around 20mb of space to cache images.

This cache is persistent across launches and images get evicted when the space runs out.

The problem is - How can you know if the image is still in the cache or not?

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

If there's no way to know, then you must cache it again. This would completely defeat the purpose of the cache if you have to send it to the device every time you display it.

Edit: The API was updated to fix this in iOS 8.2 beta 2. See Dave's answer for details.

Edit2: This was fully fixed in iOS 8.2 beta 3. See John's answer for details.

2
Why would you care? Wouldn't you cache it on the iPhone app and then just always set it on the watch? The watch will then use it's cached version if it can. - Droppy
@Droppy Yes you care, if you want the watch to use the cached version, you need to use setImageNamed: if you want to send it over, you use setImage:. The Watch doesn't do the falling back for you unfortunately. If you just use setImage: all the time, why have the cache? If you use setImageNamed: you have no feedback on whether it was actually set or not. That is the issue - Jack
I would expect that you should use setImageNamed always. If it is already there, it should be smart enough not to send it again. - progrmr
@progrmr setImageNamed never sends any images to the device FYI. If your talking about addImageToCache though, I've tried that hoping it would work but no, the 2nd time you call it it goes ahead and overwrites the first cached image. - Jack
I meant addImageToCache. How do you know it will resend it over BT when it already has it in the cache? They could just send the hash to the watch to see if it's there first. We only have the simulator now, not the real thing. - progrmr

2 Answers

4
votes

In addition to Dave's answer, it's worth noting that there's also a property on WKInterfaceDevice, cachedImages, which returns an NSDictionary of all the cached images on the current device for the running application.

From the WKInterfaceDevice Class Reference :

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

1
votes

This functionality is achievable using iOS 8.2 Beta 2. In B2, the -addCachedImage:named: methods now return a BOOL. It will return YES if the image was added to the cache, and NO if it was not (because the size limit has been reached).

The cache will also not be purged by the OS; it is up to you to purge it yourself, using the appropriate -remove... method.

Thus, if your extension maintains a list of all the images it has ever sent over to the Watch, then you can definitively know what images are in the cache.