0

我在 XPC 服务中使用出色的 ASIHTTPRequest 从 Internet 获取一些数据。

我正在创建一个简单的请求。但是,由于某种原因,它的委托函数没有被调用。我启用了 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];

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

我应该如何调整代码以便它调用委托的方法,而不修改它的逻辑/线程?

4

1 回答 1

0

I would suggest converting the statement:

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

into this, which should be semantically equivalent:

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

or simply this, which should also work:

dispatch_sync(dispatch_get_main_queue(),
                   ^{
                       [self reportFinished];
                   });
于 2014-07-09T08:21:15.007 回答