數組中的URL被一個接一個地調用。是不是應該一次性調用,比如nsoperationqueue?請幫助我,謝謝當使用dispatch_group_async時塊被逐個觸發
- (void) allTasksDone {
NSLog(@"DONE");
}
- (void) callMultiple {
dispatch_queue_t myQueue = dispatch_queue_create("com.mycompany.myqueue", 0);
dispatch_group_t group = dispatch_group_create();
NSArray *urls = [NSArray arrayWithObjects:
@"http://www.a.com",
@"http://www.b.com",
@"http://www.c.com",
nil];
for (NSString *url in urls) {
dispatch_group_async(group, myQueue, ^{
NSLog(url);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSHTTPURLResponse *response = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"COMPLETE");
});
}
dispatch_group_notify(group, myQueue, ^{
[self allTasksDone];
});
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self callMultiple];
[self.window makeKeyAndVisible];
return YES;
}
NSURLConnection sendSynchronousRequest – Vjy 2011-01-06 22:04:11