I’m building a macOS app that displays a large grid of image thumbnails similar to Photos.
What is the recommended way to determine whether a file is locally available/downloaded without making a blocking call to the filesystem for every thumbnail?
Thanks!
For security-scoped-bookmark files, the best way is to track availability with the URL resource values plus an NSFilePresenter. The protocol has tools for monitoring cloud state changes:
@property (readonly, strong) NSSet<NSURLResourceKey> *observedPresentedItemUbiquityAttributes; // macOS 10.13+
- (void)presentedItemDidChangeUbiquityAttributes:(NSSet<NSURLResourceKey> *)attributes;
You'll need to do some of the wiring yourself to keep things off the main thread:
- Cells can read an in-memory cache synchronously
- presentedItemDidChangeUbiquityAttributes: → re-fetch that item, update cache, refresh cell
- Local files: check isUbiquitousItemKey once at bookmark resolution, classify as local.
For files inside your own iCloud container, NSMetadataQuery (NSMetadataQueryUbiquitousDocumentsScope) pushes batched update deltas with the cloud-state keys.
Some docs which may help:
- NSFilePresenter — https://developer.apple.com/documentation/foundation/nsfilepresenter
- observedPresentedItemUbiquityAttributes — https://developer.apple.com/documentation/foundation/nsfilepresenter/observedpresenteditemubiquityattributes
- ubiquitousItemDownloadingStatus — https://developer.apple.com/documentation/foundation/urlresourcevalues/ubiquitousitemdownloadingstatus
- NSMetadataQuery — https://developer.apple.com/documentation/foundation/nsmetadataquery