【问题标题】:Testing Async downloads with ASIHTTPRequest使用 ASIHTTPRequest 测试异步下载
【发布时间】:2010-05-25 07:09:42
【问题描述】:

我正在使用我正在获取的ASIHTTPRequest 编写一个简单的库 以异步方式的 URL。我的问题是我的 main 函数 在异步调用完成之前写来测试我的库退出。

我是 Obj C 和 iPhone 开发的新手,谁能推荐一个 在所有请求完成之前等待的好方法 功能?

目前,我的main 函数看起来像这样 -

int main(int argc, char *argv[]) {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  IBGApp *ibgapp = [[IBGApp alloc] init];
  IBGLib *ibgl = [[IBGLib alloc] initWithUsername:@"joe" andPassword:@"xxx"];

  // The two method calls below download URLs async.
  [ibgl downloadURL:@"http://yahoo.com/" withRequestDelegate:ibgapp andRequestSelector:@selector(logData:)];
  [ibgl downloadURL:@"http://google.com/" withRequestDelegate:ibgapp andRequestSelector:@selector(logData:)];

  [pool release];
  return 0; // I reach here before the async calls are done.
}

那么等待异步调用完成的最佳方法是什么?我试过让睡眠,但显然不起作用。

【问题讨论】:

    标签: iphone objective-c macos network-programming asihttprequest


    【解决方案1】:

    您还可以告诉 ASIHTTPRequest 对象同步执行这些请求。

    [request startSynchronous];
    

    【讨论】:

    • 这是最好的主意:)。
    【解决方案2】:

    简单而肮脏的解决方案是在[池释放]之前添加一个循环;线。某事。像这样:

    while(1){
        if ([ibgl requests_finished]){
            break;
        }
    }
    

    当然,您需要将 requests_finished 布尔值添加到 IBGLib 类,并在下载完成时使其为真 (YES)。记得处理请求中的失败:)。

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2011-03-21
      相关资源
      最近更新 更多