使用ASIHTTPRequest测试异步下载

时间:2010-05-25 07:09:43

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

我正在使用ASIHTTPRequest编写一个简单的库 URL以异步方式。我的问题是我main的功能 已编写以在异步调用完成之前测试我的lib出口。

我是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.
}

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

2 个答案:

答案 0 :(得分:2)

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

[request startSynchronous];

答案 1 :(得分:1)

简单而肮脏的解决方案是在[pool release]之前添加一个循环;线。 STH。像这样:

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

当然,您需要在IBGLib类中添加requests_finished布尔值,并在下载完成后将其设置为true(YES)。请记住处理请求中的失败:)。