我正在使用iOS AWS SDK上传和下载本地硬盘到Amazon S3存储的文件。我能够完成这项工作,但我无法让S3代表在操作完成或导致错误时正确响应以提醒我。
我有一系列要上传的文件。对于每个文件,我创建了一个NSOperation
,其中main
例程主要包括:
AmazonCredentials * credentials = [[AmazonCredentials alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
putObjectRequest = [[S3PutObjectRequest alloc] initWithKey:pathCopy inBucket:[self bucket]];
putObjectRequest.filename = pathSource;
putObjectRequest.credentials=credentials;
[putObjectRequest setDelegate:s3Delegate];
此处,委托(s3Delegate
)创建为常规AmazonServiceRequestDelegate,应该能够在操作完成时触发响应。我的NSOperations
中的每一个都添加到我的NSOperationQueue
中,后者非同时执行操作。如果我使用委托[putObjectRequest setDelegate:s3Delegate]
,则操作无效。如果我删除了委托的使用,则操作正确执行但我无法接收任何操作响应,因为我没有委托。
如果我完全删除NSOperationQueue
的使用并使用[putObjectRequest setDelegate:s3Delegate]
,则委托可以完美运行。
我的问题是在队列中使用委托时我做错了什么?由于委托完全能够执行而不在队列中,这可能与不在主线程上执行有关吗?我真的希望能够使用队列来限制非并发操作的数量,但我无法弄清楚这一点。我希望有人知道这里发生了什么,任何示例代码将不胜感激。谢谢! 干杯,特隆德
答案 0 :(得分:8)
在您设置委托之后,似乎aws sdk的行为异步。 因此,为了使您的异步aws工作在(异步)NSOperation中工作,您需要花些时间等待AWS完成:
在你的.h NSOperation文件中,添加一个布尔值:
@interface UploadOperation : NSOperation <AmazonServiceRequestDelegate> {
@private
BOOL _doneUploadingToS3;
}
在您的.m文件中,您的main方法如下所示:
- (void) main
{
.... do your stuff …..
_doneUploadingToS3 = NO;
S3PutObjectRequest *por = nil;
AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY withSecretKey:SECRET_KEY];
s3Client.endpoint = endpoint;
@try {
por = [[[S3PutObjectRequest alloc] initWithKey:KEY inBucket:BUCKET] autorelease];
por.delegate = self;
por.contentType = @"image/jpeg";
por.data = _imageData;
[s3Client putObject:por];
}
@catch (AmazonClientException *exception) {
_doneUploadingToS3 = YES;
}
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!_doneUploadingToS3);
por.delegate = nil;
.... continue with your stuff ….
}
不要忘记实现您的委托方法
-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{
_doneUploadingToS3 = YES;
}
-(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error
{
_doneUploadingToS3 = YES;
}
-(void)request:(AmazonServiceRequest *)request didFailWithServiceException:(NSException *)exception
{
_doneUploadingToS3 = YES;
}
- (void) request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
// Do what you want
}
-(void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)response
{
// Do what you want
}
-(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data
{
// Do what you want
}
注意:这种魔法适用于任何异步执行但必须在NSOperation中实现的内容。