ASIHTTPRequest-多个请求

时间:2011-05-31 21:26:41

标签: iphone facebook asihttprequest

我正在开发一个应用程序,它将从Facebook服务器请求某些用户的个人资料图片URL,但我不知道我将拥有多少用户(可能是2或者可能是20)。我应该使用带有循环和同步请求的ASIHTTPRequest,还是带有循环的API图表(带有Facebook SDK for iOS)?

1 个答案:

答案 0 :(得分:7)

尝试使用ASINetworkQueue。它将允许您创建仍可以异步启动的ASIHTTPRequests队列。例如

- (void)getImages
{
    if(!self.queue)
        self.queue = [[[ASINetworkQueue alloc] init] autorelease];

    NSArray* urlStringsToRequest = [NSArray arrayWithObjects:@"http://www.example.com/image1.png",@"http://www.example.com/image2.png",nil];
    for(NSString* urlString in urlStringsToRequest)
    {
        NSURL *url = [NSURL URLWithString:urlString];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(requestDone:)];
        [request setDidFailSelector:@selector(requestWentWrong:)];
        [self.queue addOperation:request];
    }

    [self.queue go];
}

- (void)requestDone:(ASIHTTPRequest*)req
{
    UIImage* image = [UIImage imageWithData:[req responseData]];

    [imageArray addObject:image];
}

- (void)requestWentWrong:(ASIHTTPRequest*)req
{
    NSLog(@"Request returned an error %@",[req error]);
}