Using delegates, operations, and queues

I use the AWS SDK for iOS to upload and download files from the local hard drive to and from the local Amazon S3 drive. I can do this work, but I can’t get the S3 delegate to respond correctly in order to warn me of an operation being completed or an error.

I have an array of files that I want to upload. For each file, I create NSOperationwhere the procedure mainconsists mainly 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];

Here, the delegate ( s3Delegate) is created as a regular AmazonServiceRequestDelegate , which should be able to turn off responses when the operation is completed. Each of mine is NSOperationsadded to mine NSOperationQueue, which does not perform operations simultaneously. If I use a delegate [putObjectRequest setDelegate:s3Delegate], operations do not work. If I remove the use of the delegate, the operations will be performed correctly, but I can not get answers to the operations, because I do not have a delegate.

If I completely remove the use NSOperationQueueand use [putObjectRequest setDelegate:s3Delegate], the delegate works fine.

, ? , , , ? , . , - , , - . ! Cheers, Trond

+1
1

, aws sdk , . , aws () NSOperation, , AWS:

.h NSOperation boolean:

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

.m :

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

+8

All Articles