小编Ash*_*shu的帖子

GCD与NSURLConnection

GCD用来HTTP异步发送请求.这是不起作用的代码:

dispatch_async(connectionQueue, ^{
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

        [request setURL:[NSURL URLWithString:[NSString stringWithFormat:someURL]]];


        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];//Not working
    }); 
Run Code Online (Sandbox Code Playgroud)

上面的代码根本不起作用.我没有在NSURLConnectionDelegate的方法中收到任何回调.

但是,当我尝试以下代码时,一切正常,我得到了适当的回调

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:someURL]]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

dispatch_async(connectionQueue, ^{

    [connection start]; // working fine. But WHY ????
});
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下块/ GCD的奇怪行为吗?

iphone grand-central-dispatch ios objective-c-blocks

7
推荐指数
1
解决办法
2838
查看次数