我正在使用这种方法将图像/视频上传到 S3(参见下面的代码)
我想知道用户是否将应用程序设置为背景并在很久以后将其打开备份是否可以使用暂停/恢复来恢复所有这些上传?看起来它可能会在 SDK 中使用self.cache.diskCache
. 换句话说,我可以使用 UIBackgroundTask 来暂停到期处理程序中的下载以及应用程序何时返回前台 resumeAll 吗?
我正在观看有关如何使用 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;
}];