handling files offloaded to iCloud?

My app saves its document files by default into ~/Documents. It does some important domain-specific stuff when a document is deleted. I monitor for deletion using https://github.com/eonist/FileWatcher

Unfortunately several users have noticed my app doing this cleanup work even when they have not deleted the corresponding document. We've traced it through and realised it's the iCloud "Optimise Mac Storage" feature, or "Store in iCloud > Desktop and Documents". I'm not sure which because I don't use these features of macOS at all, and also they seem to have been renamed or changed in Sonoma.

Either way, I'm wondering:

a) how I can tell in Swift whether a file has actually been deleted, or whether it's been "offloaded" to iCloud by macOS.

b) how can I test this?

My research is pointing at urlubiquitousitemdownloadingstatus but it's hard to play with it without knowing how to test it.

Are you talking about the Remove Downloaded feature on macOS Sonoma? In the Finder of macOS Sonoma, if you right click an iCloud Drive file, the contextual menu has a sub menu named "Remove Downloaded", which allows you to remove the file content from the local hard disk and saves the space.

Apps can use evictUbiquitousItem(at:) to programmatically remove the downloaded content for an iCloud file as well.

Removing the downloadeded content of an iCloud file is different from deleting it, in that the file metadata is still there after the removal, and if you access the file, via user interaction or with code, the system will automatically download the file content and make it available to you. For more information, see TN3150: Getting ready for dataless files.

I am unclear how FileWatch works, and hence can't explain why removing the downloadeded content triggers your cleanup code. If you can share the details about how FileWatch detects the file deletion, I may be able to comment.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks Ziqiao,

I'm not talking about the "Remove Downloaded" feature, but it's definitely something very similar.

In the problem scenario, the files are stored in ~/Documents/x and if the users' hard drives get too full, macOS is moving those documents to iCloud automatically, thereby triggering the FileWatcher* to say they've been removed.

I believe that behind-the-scenes "Optimise Mac Storage" almost certainly uses the same mechanism as the "Remove Downloaded" feature you mentioned (ie 'dataless files") which I'd never heard of till now. Your tech note link gave me some good keywords for further research.

If it's true I'm dealing with a dataless file issue, when FileWatcher says a file was deleted can I simply ask fileExists(atPath:) to see if it's actually still there or not? I think a dataless file will still return true for a fileExists call, despite being offloaded?

* FileWatcher is just using FSEvents, I believe. Pertinent code is here.

handling files offloaded to iCloud?
 
 
Q