ASIHTTPRequestDelegate方法未在XPC服务中调用

时间:2014-07-09 07:36:23

标签: objective-c macos asihttprequest xpc

我在XPC服务中使用优秀的ASIHTTPRequest来从互联网上获取一些数据。

我正在创建一个简单的请求。但是,出于某种原因,它的委托函数不会被调用。 我启用了ASIHTTPRequest的调试日志,所以我看到请求确​​实执行了。 在深入挖掘之后,我发现了可能的原因:

在ASIHTTPRequest.m中,在requestFinished方法的末尾,有一个电话:

[self performSelectorOnMainThread:@selector(reportFinished) withObject:nil waitUntilDone:[NSThread isMainThread]];

reportFinished方法是调用委托的方法。 出于某种原因,它永远不会被解雇。如果我将此行替换为:

//[self performSelectorOnMainThread:@selector(reportFinished) withObject:nil waitUntilDone:[NSThread isMainThread]];
[self reportFinished];

然后调用委托。但是,我假设它在错误的线程上被调用为运行异步请求。

我应该怎么做才能调整代码,以便调用代理的方法,而不修改代码的逻辑/线程?

1 个答案:

答案 0 :(得分:0)

我建议转换声明:

[self performSelectorOnMainThread:@selector(reportFinished) withObject:nil waitUntilDone:[NSThread isMainThread]];

进入这个,它应该在语义上等价:

if ([NSThread isMainThread]) {
    [self reportFinished];
} else {
    dispatch_sync(dispatch_get_main_queue(),
                   ^{
                       [self reportFinished];
                   });
}

或者只是这个,这也应该有效:

dispatch_sync(dispatch_get_main_queue(),
                   ^{
                       [self reportFinished];
                   });