1

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;
                                                   }];
tettoffensive
  • 664
  • 7
  • 24
  • `AWSS3TransferUtility` supports background transfers; however, `AWSS3TransferManager` does not. We focus our development effort on `AWSS3TransferUtility` and plan to phase out `AWSS3TransferManager` support. We recommend starting looking into `AWSS3TransferUtility` and see if it covers your needs. – Yosuke Nov 12 '15 at 01:36
  • good to know that it's going to be phased out. I will take a look – tettoffensive Nov 12 '15 at 18:25

0 Answers0