可以在后台线程上从ASIHTTPREQUEST / AFnetworking执行offerionBlock吗?
-
13-11-2019 - |
题
我在这样的单独线程中同步启动我的ashtperquest:
//inside this start method I create my ASIHTTPRequest and start it
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[process start];
});
.
但仍在主线程上播放正确的块。可以一个人继续在与请求启动的同一线程中执行正确的函数? 我唯一可以提出解决这个问题的是定义调度队列并在同一线程中手动执行CompleitionBlock,从而保持对创建的队列的引用。但这并没有直接解决它,因为在将代码的其余部分重定向到创建的调度队列之前,您将在主线程中传递一个疯狂的小时刻。
有人有更好的解决方案吗?
编辑:AFnetworking完成块也是如此,也是如此...
解决方案
而是可以使用AFnetwork框架。在这里,您有两个属性在任何类型的文件中称为' successcallbackqueue '和' fallurecallbackqueue '。在哪里可以添加预定义的' dispatch_queue_t '来处理成功和失败块的执行。
希望这将有助于别人同样的问题!
更新:示例
. dispatch_queue_t requestQueue = dispatch_queue_create("requestQueue", NULL);
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:yourRequest];
operation.successCallbackQueue = requestQueue;
operation.failureCallbackQueue = requestQueue;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// add code for completion
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// add code for failure
}];
[operation start];
其他提示
尝试使用自己创建的定义队列。然后,您可以(一旦它最终完成,在其完成块)信号返回全局队列以更新所需的任何显示。