【问题标题】:How can I add a listener for when my S3PutObjectRequest has completed?当我的 S3PutObjectRequest 完成时,如何添加侦听器?
【发布时间】:2012-06-13 10:42:33
【问题描述】:

我正在使用运行良好的 iOS SDK 上传到 Amazon S3,但我希望能够在加载完成时触发一个方法。

这是我的代码:

AmazonS3Client *s3 = [[[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY] autorelease];
// Create the picture bucket.
[s3 createBucket:[[[S3CreateBucketRequest alloc] initWithName:[Constants pictureBucket]] autorelease]];
NSString *picName = [NSString stringWithFormat:@"%@%d", PICTURE_NAME, counter];
// Upload image data.  Remember to set the content type.
S3PutObjectRequest *por = [[[S3PutObjectRequest alloc] initWithKey:picName inBucket:[Constants pictureBucket]] autorelease];
NSLog(@"------------ SUBMITTING img :%@", picName);
por.contentType = @"image/jpeg";
por.data        = imageData;
counter++;                   
// Put the image data into the specified s3 bucket and object.
[s3 putObject:por];

任何帮助都非常感谢!

【问题讨论】:

    标签: objective-c ios amazon-s3 amazon


    【解决方案1】:

    Amazon SDK Docs 看来你得到了一个S3PutObjectResponse

    所以

    S3PutObjectResponse *response  = [s3 putObject:por];
    if ([response isFinishedLoading]) {
        //do something
    }
    

    或者您可能正在搜索 connectionDidFinishLoading:,它是来自 NSURLConnection 的委托方法,它们似乎相应地使用 AmazonServiceResponse Class Reference

    在你的.h文件中声明你符合NSURLConnection的委托协议

    @interface MyClass : NSObject <NSURLConnectionDelegate>
    

    在你的 .m 文件中实现你想要的委托方法

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
          //do your stuff here
    }
    

    并告诉 NSURLConnection 你在 .m 文件中处理委托方法

    S3PutObjectRequest *por = [[[S3PutObjectRequest alloc] initWithKey:picName inBucket:[Constants pictureBucket]] autorelease];
    por.urlRequest.delegate = self; // this is important !!!
    

    一般来说,您应该习惯使用代理,因为它们经常通过 iOS SDK 漏洞使用!!

    您可以在此处找到其他文档:Delegates and Data Sources

    【讨论】:

    • 感谢 Pfitz,您的示例效果很好,您不能指点我使用 connectionDidFinishLoading 的其他技术示例吗?抱歉,我对此很陌生!
    • 太棒了,谢谢!我自己也想不通
    • 嗯,我刚试过这个,但我似乎无法访问属性 por.urlRequest.delegate,我在 'AmazonURlRequest' 上找不到错误 'property 'delegate' 你是对的我需要学习代表!将通读文档,再次感谢
    • 我发现我可以使用此代码来设置委托,尽管我留下了一个未使用的变量:NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:por.urlRequest delegate:self] ;我会阅读代表!
    • 我自己没用过amazon sdk。但它明确指出应该有一个属性委托 - 你可以尝试'por.delegate = self';
    【解决方案2】:

    我还有一件事要添加到 cmets 中(我知道我在这里不守规矩,但代表阻止我发表评论)。为了安全起见,我运行了这两行,因为我发现第一行并没有始终如一地保持其价值:

        por.delegate = self;
        [por setDelegate:self];
    

    由于您像我一样是新手,所以委托本质上是处理程序,当对象调用有时需要或不需要的强制性方法时,它看起来是这样的。如果您将委托设置为self,则意味着 putObjectRequest 将在调用 self 时引用强制方法,例如 Pfitz 的答案中的方法。对于UITableView,委托方法的一个示例是(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,其中对象UITableView 将引用self 以查找方法cellForRowAtIndexPath 以填充其对象的单元队列。

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 2011-08-21
      • 1970-01-01
      • 2013-04-23
      • 2020-06-10
      • 1970-01-01
      相关资源
      最近更新 更多