NSURLConnection和Blocks

时间:2011-12-01 23:02:30

标签: objective-c nsurlconnection block

我的回调未被调用。我不熟悉dispatch_async所以有什么想法吗?重点是,我从未看到过响应。

+ (void)asyncRequest:(NSURLRequest *)request
         success:(void(^)(NSData *, NSURLResponse *))successBlock_ 
         failure:(void(^)(NSData *, NSError *))failureBlock_ 
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSURLResponse *response = nil;
    NSError *error = nil;

    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response 
                                                     error:&error];

    NSString *result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"-----------------result %@", result);

    if (error) {
        failureBlock_(data,error);
    } else {
        successBlock_(data,response);
    }
    [pool release];
});
}

1 个答案:

答案 0 :(得分:1)

为什么要实现自己的sendAsync方法?

您应该考虑使用提供的NSURLClassReference

修改

假设您正在为OSX开发,而不是iOS。显然这种方法不适用于iOS。