<!--
{
  "availability" : [
    "macOS: 10.7.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocument/autosave(withImplicitCancellability:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocument(im)autosaveWithImplicitCancellability:completionHandler:"
  },
  "title" : "autosave(withImplicitCancellability:completionHandler:)"
}
-->

# autosave(withImplicitCancellability:completionHandler:)

Autosaves the document’s contents to an appropriate file-system location, as needed.

```
func autosave(withImplicitCancellability autosavingIsImplicitlyCancellable: Bool, completionHandler: @escaping ((any Error)?) -> Void)
```

## Parameters

`autosavingIsImplicitlyCancellable`

The value in the [`autosavingIsImplicitlyCancellable`](/documentation/AppKit/NSDocument/autosavingIsImplicitlyCancellable) property while autosaving is happening.

`completionHandler`

The completion handler block object passed in to be invoked at some point in the future, perhaps after the method invocation has returned. The completion handler must be invoked on the main thread.

    The block takes one argument:

- `errorOrNil`: If successful, pass a `nil` error. If not successful, pass an `NSError` object that encapsulates the reason why the document could not be autosaved.

## Discussion

The default implementation of this method does the following:

1. Checks the value of the [`hasUnautosavedChanges`](/documentation/AppKit/NSDocument/hasUnautosavedChanges) property.
2. If the value of that property is <doc://com.apple.documentation/documentation/Swift/false>, the method runs the completion handler with a `nil` error and returns immediately.

If the value is <doc://com.apple.documentation/documentation/Swift/true>, calls [`autosavesInPlace`](/documentation/AppKit/NSDocument/autosavesInPlace) on the class to determine where the autosaved document contents should go.

The method also gets the value in [`fileURL`](/documentation/AppKit/NSDocument/fileURL) to ensure that the file has an actual URL, because it is not possible to autosave in place if the document does not yet have a permanent location.
3. Checks the value in the [`autosavingFileType`](/documentation/AppKit/NSDocument/autosavingFileType) property to determine the file type for the autosaved file.
4. Calls [`save(to:ofType:for:completionHandler:)`](/documentation/AppKit/NSDocument/save(to:ofType:for:completionHandler:)).

The value of the `saveToURL` parameter is the location where the file should be saved. If the file has a URL and the class specifies that autosave should occur in place, this is the URL of the file. Otherwise, this is the location of a nonexistent file in the specified autosave location.

The value for the `ofType` parameter is determined by a call to [`autosavingFileType`](/documentation/AppKit/NSDocument/autosavingFileType).

The value of the `forSaveOperation` parameter is `NSAutosaveInPlaceOperation` if the class is configured to autosave in place and the file has a URL. Otherwise, the value is `NSAutosaveElsewhereOperation`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)