【问题标题】:Return Value of method using AsiHTTPRequest使用 AsiHTTPRequest 的方法的返回值
【发布时间】:2012-02-21 12:02:37
【问题描述】:

我有一个使用 AsiHTTPRequest 的类。我想做一个这样的方法:

-(NSData*)downloadImageFrom: (NSString*)urlString;
{
    // Set reponse data to nil for this request in the dictionary
    NSMutableData *responseData = [[NSMutableData alloc] init];
    [responseDataForUrl setValue:responseData forKey:urlString];

    // Make the request
    NSURL *url = [NSURL URLWithString:urlString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [responseDataForUrl setValue:responseData forKey:[request url]];
    [request setDelegate:self];
    [request startAsynchronous];

    // Wait until request is finished (???? Polling ?????)
    // while(responsedata = nil) {
    //    do nothing
    // } BAD SOLUTION

    return responseData;

}

之后。当 responseData 准备好时调用委托方法。有没有比在变量 responseData 中轮询更好的解决方案?

【问题讨论】:

    标签: iphone ios cocoa asihttprequest


    【解决方案1】:

    我在大多数 Web 服务调用中使用 ASIHttpRequest,但在您的情况下(获取图像数据异步),我使用 GCD 块。我有一个名为 WebImageOperations 的类,在该类中我有一个类方法:

    WebImageOperations.h:

    + (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage;
    

    WebImageOperations.m:

    + (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage
    {
        NSURL *url = [NSURL URLWithString:urlString];
    
        dispatch_queue_t callerQueue = dispatch_get_current_queue();
        dispatch_queue_t downloadQueue = dispatch_queue_create("com.achcentral.processimagedataqueue", NULL);
        dispatch_async(downloadQueue, ^{
            NSData * imageData = [NSData dataWithContentsOfURL:url];
    
            dispatch_async(callerQueue, ^{
                processImage(imageData);
            });
        });
        dispatch_release(downloadQueue);
    }
    

    然后调用它,使用这个:

    [WebImageOperations processImageDataWithURLString:@"MyURLForPicture" andBlock:^(NSData *imageData) {
            if (self.view.window) {
                 UIImage *image = [UIImage imageWithData:imageData];
                 self.myImageView.image = image;
            }
    }];
    

    【讨论】:

      【解决方案2】:

      您的委托不必是一个单独的类。

      - (void)someMethod:(NSUrl *)url
      {
          ASIHTTPRequest *req = [ASIHTTPRequest requestWithUrl:url];
          [req setDelegate:self];
          //configure the request
          [req startAsynchronous];
      }
      
      - (void)requestDone:(ASIHTTPRequest *)request
      {
         NSString *response = [request responseString];
         //do whatever with the response
      }
      

      所以您的方法someMethod: 触发请求并返回无效。当请求完成时,您的 ASIHTTPRequest 会在其委托上触发 requestDone: 方法,该委托是同一个对象。在该方法中,您可以执行任何操作——设置 ivar 或命名属性、处理传入数据并填充 UITableVew,等等。

      请注意,ASIHTTPRequest 现在已被弃用,其作者建议改用其他东西。 AFNetworking 似乎是一个受欢迎的选择,但我最近没有开始一个新项目,所以我自己还没有选择一个。

      【讨论】:

        【解决方案3】:

        你绝对不应该做投票!

        您设置为 self 的 ASIHHTPRequest 的委托将调用一个方法(有关该委托方法的详细信息,请参阅 ASIHTTPRequest 文档)以在完成时通知您。在该委托方法中,调用您想要执行的任何其他代码。不必费心返回图像 - 这都是异步的。

        【讨论】:

        • 我知道代表。那么,我怎样才能编写一个单独的类来实现我想要的这个方法呢?我必须以某种方式返回。
        • 为什么一定要退货?只需将委派添加到您的课程中。如果您使用异步调用编写代码,则必须相应地调整编码和设计。
        猜你喜欢
        • 1970-01-01
        • 2014-04-12
        • 2023-03-09
        • 2015-01-20
        • 1970-01-01
        • 2021-12-14
        • 2020-06-10
        • 2014-10-18
        相关资源
        最近更新 更多