Skip to content

Commit

Permalink
Merge pull request #3664 from aws-amplify/stehlib.transfer-utility-re…
Browse files Browse the repository at this point in the history
…sume

updates docs on how to auto-resume s3 network tasks
  • Loading branch information
brennanMKE authored Nov 8, 2021
2 parents dbaafe6 + c1ce797 commit a8991d3
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/fragments/sdk/storage/ios/transfer-utility.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,30 @@ func application(_ application: UIApplication, handleEventsForBackgroundURLSessi

## Managing Transfers When an App Restarts

When an app that has initiated a transfer restarts (if it has been terminated by the system and not force-closed), it is possible that the transfer may still be in progress or completed. To make the restarting app aware of the status of transfers, instantiate the transfer utility using the ``AWSS3TransferUtility.s3TransferUtility(forKey: "YOUR_KEY")`` method. AWSS3TransferUtility uses the key to uniquely identify the NSURLSession of the transfers initiated by the app, so it is important to always use the same identifier. AWSS3TransferUtility will automatically reconnect to the transfers that were in progress the last time the app was running.
When an app that has initiated a transfer restarts (if it has been terminated by the system and not force-closed), it is possible that the transfer may still be in progress or completed. Tasks for uploads, downloads and multipart uploads can be monitored with progress and completion handlers. In the code below accessing the default instance of the Transfer Utility has a completion handler which is run once the recovery process has completed. Any network operations which were still running and were persisted by Transfer Utility are restored. Once the completion handler is done it is ready to attach handlers for progress and completion updates.

Though it can be called anywhere in the app, we recommend that you instantiate the AWSS3TransferUtility in the ``appDidFinishLaunching`` lifecycle method.
```swift
init() {
transferUtility = AWSS3TransferUtility.default(completionHandler: { error in
if let error = error {
print("Error: \(error)")
return
}
self.reattachHandlers()
})
}
func reattachHandlers() {
let blocks = AWSS3TransferUtilityBlocks(uploadProgress: nil,
multiPartUploadProgress: handleProgress(task:progress:),
downloadProgress: nil,
uploadCompleted: nil,
multiPartUploadCompleted: handleCompletion(task:error:),
downloadCompleted: nil)
transferUtility.enumerateToAssign(blocks: blocks)
}
```

This code is only attaching handlers for multipart uploads. The other handlers are left as `nil` but they could be defined as well depending on your needs. Handlers are given the `task` which includes the `transferID` property which can be used to associate with network operations. Place this initialization routine at the start of your app launch lifecycle.

## Manage a Transfer when a Suspended App Returns to the Foreground

Expand Down

0 comments on commit a8991d3

Please sign in to comment.