使用委托,操作和队列

编程入门 行业动态 更新时间:2024-10-27 10:22:53
本文介绍了使用委托,操作和队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是 AWS SDK 的iOS上传和下载文件,并从当地硬盘驱动器到Amazon S3存储。我有能力做这项工作,但我无法得到S3代表回应时正确操作完成后,或产生错误来提醒我。

I am using the AWS SDK for iOS to upload and download files to and from local hard drive to Amazon S3 storage. I am capable of making this work but I am unable to get the S3 delegate to respond properly to alert me when operations have finished or resulted in an error.

我有我要上传文件的数组。对于每一个文件创建一个的NSOperation 其中主程序由大量的:

I have an array of files that I want to upload. For each file I create a NSOperation where the main routine consist mostly of:

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] 的操作无法正常工作。如果删除了使用委托的操作都正确执行,但我无法得到任何回复的操作,因为我没有一个代表。

Here, the delegate (s3Delegate) is created as a regular AmazonServiceRequestDelegate which should be able to fire off responses when an operation has finished. Each of my NSOperations are added to my NSOperationQueue which executes operations non-concurrently. If I use the delegate [putObjectRequest setDelegate:s3Delegate] the operations are not working. If I remove the use of the delegate the operations are performed correctly but I am unable to receive any responses to the operations as I do not have a delegate.

如果我删除了使用 NSOperationQueue 完全并使用 [putObjectRequest setDelegate:s3Delegate] 的代表作品完美。

If I remove the use of the NSOperationQueue completely and use the [putObjectRequest setDelegate:s3Delegate] the delegate works perfectly.

我的问题是我在做什么错用为代表的队列?由于委托是完全有能力执行而不是在一个队列会这样涉及到不执行​​主线程上的?我真的希望能够使用队列来限制非并发操作数,但是我无法想出解决办法。我希望有人有什么是怎么回事的想法,任何例如code会大大AP preciated。谢谢! 欢呼声中,特龙

My question is what am I doing wrong with using a delegate in a queue? Since the delegate is perfectly capable of performing while not in a queue could this be related to not performing on the main thread? I really want to be able to use the queue to limit the number of non-concurrent operations, however I am unable to figure this out. I hope someone has an idea of what is going on here and any example code would be greatly appreciated. Thanks! Cheers, Trond

推荐答案

看来,AWS SDK您设置的委托时间之后的行为是异步的。 因此,为了让您的异步AWS的东西的工作中(异步)的NSOperation,你得把一些魔法等待AWS来完成:

It seems that the aws sdk behaves asynchronously after the time you set your delegate. So in order to have your asynchronous aws stuff work in a (asynchronous) NSOperation, you got to put some magic to wait for AWS to complete:

在您的.H的NSOperation文件,添加一个布尔值:

In your .h NSOperation file, add a boolean:

@interface UploadOperation : NSOperation <AmazonServiceRequestDelegate> { @private BOOL _doneUploadingToS3; }

和你的.m文件,你的主要方法是这样的:

and in your .m file, your main method will look like this:

- (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 …. }

不要忘记实现你的委托方法

do not forget to implement your delegate methods

-(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实施

Note: this magic can work for any stuff that performs asynchronously but have to be implemented in a NSOperation.

更多推荐

使用委托,操作和队列

本文发布于:2023-10-13 17:09:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488579.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!