使用暂停/恢复持久上传到s3当应用程序背景时?

时间:2015-11-12 00:03:48

标签: ios amazon-web-services amazon-s3 uibackgroundtask

我正在使用this method to upload images/videos to S3(请参阅下面的代码)

我想知道一个用户是否对应用程序进行了背景,并且稍后将其重新打开,可以使用暂停/恢复来恢复所有这些上传吗? 看起来它可能会持续使用self.cache.diskCache缓存SDK中的上传内容。换句话说,我可以使用UIBackgroundTask来暂停过期处理程序中的下载以及当应用程序返回时前台resumeAll?

我正在观看this talk on how to do persistent uploads with NSURLSession,并且正在设法在我当前的应用中设计一个好方法。

这样的事情:

- (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;
    }];
}

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;
                                                   }];

0 个答案:

没有答案