Detection of file eviction

I want to implement quota feature to my file provider extension. I am able to keep track of materialized files total size. (Content download and edit operations)

However I cannot detect file eviction operation (User right click to file and select "Remove Download"). Is there anyway to detect this action

Or any suggestion to keep track of materialized files total size?

Answered by Engineer in 832660022

You can use the Materialized Items enumerator, to be informed when items are materialized / evicted.

See: https://developer.apple.com/documentation/fileprovider/nsfileprovidermanager/enumeratorformaterializeditems()?changes=_1&language=objc

You can use the Materialized Items enumerator, to be informed when items are materialized / evicted.

See: https://developer.apple.com/documentation/fileprovider/nsfileprovidermanager/enumeratorformaterializeditems()?changes=_1&language=objc

Yeah i saw this but i was not understand or find how to use it. i use this function and get an enumerator for materialized items but how am i supposed to use this enumerator to get list of materialized items? Can you give me an example or describe how to do it?

My colleague hasn't responded the above comment, and so I'm joining this discussion.

The materialized items enumerator can be used with materializedItemsDidChange(completionHandler:) to track the materialized item set in the following way:

  1. The system calls materializedItemsDidChange(completionHandler:) when the materialized item set changes.

  2. You grab the materialized items enumerator, and then call enumerateItems(for:startingAt:) to trigger an enumeration.

  3. The system delivers the updated items to the observer NSFileProviderEnumerationObserver by calling didEnumerate(_:).

Assuming that you use the extension as the observer, the code is like the following:

extension Extension: NSFileProviderEnumerationObserver {
public func didEnumerate(_ updatedItems: [any NSFileProviderItemProtocol]) {
for item in updatedItems {
print("item.isDownloaded = \(String(describing: item.isDownloaded))")
}
}
public func finishEnumerating(upTo nextPage: NSFileProviderPage?) {
print("\(#function)")
}
public func finishEnumeratingWithError(_ error: any Error) {
print("\(#function): \(error)")
}
// materializedItemsDidChange is a method of NSFileProviderReplicatedExtension.
// Assume that Extension implements NSFileProviderReplicatedExtension.
public func materializedItemsDidChange(completionHandler: @escaping () -> Void) {
print("\(#function)")
let fileProviderManager = NSFileProviderManager(for: domain)
if let enumerator = fileProviderManager?.enumeratorForMaterializedItems() {
enumerator.enumerateItems(for: self, startingAt: NSData() as NSFileProviderPage)
}
completionHandler()
}
}

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Detection of file eviction
 
 
Q