这段代码意味着什么?
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
TMBaseParser *parser=[[TMBaseParser alloc] init];
parser.delegate=self;
NSString *post =nil;
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
[parser parseForServiceType:TMServiceCategories postdata:postData];
});
请简要解释一下。
答案 0 :(得分:101)
中的一段代码
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
在后台线程上异步运行。这样做是因为解析数据可能是一项耗时的任务,它可能会阻止主线程停止所有动画并且应用程序无法响应。
如果您想了解更多信息,请阅读Grand Central Dispatch和Dispatch Queue上的Apple文档。
答案 1 :(得分:6)
如果以上代码段不起作用,请尝试以下操作:
<强>目标-C:强>
dispatch_async(dispatch_get_main_queue(), ^{
});
应始终从主队列执行UI更新。 “^”符号表示块的开始。
斯威夫特3:
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
}
}
答案 2 :(得分:2)
这是Grand Central Dispatch block。