Contents

trashItem(withIdentifier:completionHandler:)

Moves an item into the trash.

Declaration

func trashItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping  @Sendable (NSFileProviderItem?, (any Error)?) -> Void)
func trashItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier) async throws -> NSFileProviderItem

Parameters

  • itemIdentifier:

    The item’s persistent identifier.

  • completionHandler:

    A block that takes the following parameters:

    trashedItem

    A file provider item that represents the changed item, or nil if an error occurred.

    error

    An error object. If an error occurs, pass in an object that describes the error; otherwise, set it to nil.

Discussion

This method is called when the user moves a document or directory into the trash. Override this method to make any necessary local changes to move the item to the trash, including updates to the working set. Your implementation should return immediately. Call the completion handler before performing any network activity or other long-running tasks. Defer these tasks to the background. The trashedItem instance that you pass to the completion handler should match the item’s old file provider item, with only one change: set the item’s isTrashed property to true.

Always include trashed items in your File Provider extension’s working set. Trashed items should be stored locally so that their content is available to offline users. However, if the trashed item is a directory, remove all of its children from the working set. If a shared document is trashed, stop sharing the document while it is trashed.

Be sure to record the item’s parent identifier, so that you can restore the item to its proper location in the untrashItem(withIdentifier:toParentItemIdentifier:completionHandler:) method. You can store this identifier in the trashedItem object’s parentItemIdentifier property.

The user’s ability to trash an item is controlled by the item’s allowsTrashing capability.

See Also

Handling actions