Если вы не загружаете много вещей параллельно и выполняете простой запрос GET, самый простой способ сделать это - отправить синхронный запрос в одну из глобальных очередей:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
NSURLResponse* response = nil;
NSError* error = nil;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// There will be response data in response now, like the http status code
// etc. You should check this information to make sure you didn't get a 404
// or some other http status error
if( result ) {
// you have a good result, do something with it like create a new object or
// pass it to a method on this object, etc.
dispatch_async(dispatch_get_main_queue(), ^{
[self doSomethingWithResponseData:result];
});
} else {
// You got an error making the connection, so handle it
NSLog(@"Error making connection: %@", error);
}
});
** примечание: этот пример кода использует GCD и, следовательно, будет работать только на Snow Leopard (10.6) или выше.Если вам нужно нацелиться на Leopard или Tiger, вы можете сделать то же самое, используя селекторы потоков, но не как in-line.