I'm using this method to upload images/videos to S3 (see code below)
I'm wondering if a user backgrounds the app and much later opens it back up can the pause/resume be used to resume all those uploads? It looks like maybe it's persistently caching the uploads in the SDK with self.cache.diskCache
. In other words can I use a UIBackgroundTask to pause the downloads in the expiration handler and when the app comes back in the foreground resumeAll?
I was watching this talk on how to do persistent uploads with NSURLSession and am trying to design a good way to do it in my current app.
Something like this:
- (void)applicationWillEnterForeground:(NSNotification *)notification
{
[self.transferManager resumeAll:^(AWSRequest *request) {
POLYLog(@"request %@",request.description);
}];
}
- (void)applicationDidEnterBackground:(NSNotification *)notification
{
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier task;
task = [app beginBackgroundTaskWithExpirationHandler:^{
[self.transferManager pauseAll];
[app endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
}];
}
Reference from AWS Docs:
// Construct the NSURL for the download location.
NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"downloaded-myImage.jpg"];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];
// Construct the download request.
AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
downloadRequest.bucket = @"myBucket";
downloadRequest.key = @"myImage.jpg";
downloadRequest.downloadingFileURL = downloadingFileURL;
Now we can pass the download request to the download: method of the TransferManager client. The AWS Mobile SDK for iOS uses AWSTask to support asynchronous calls to Amazon Web Services. The download: method is asynchronous and returns a AWSTask object, so we’ll use it accordingly:
[[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
withBlock:^id(AWSTask *task) {
if (task.error) {
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
break;
default:
NSLog(@"Error: %@", task.error);
break;
}
} else {
// Unknown error.
NSLog(@"Error: %@", task.error);
}
}
if (task.result) {
AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
// The file uploaded successfully.
}
return nil;
}];