下面的代码在iOS上连续崩溃。请帮助我找出错误的确切原因。
@try { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc]initWithData:[attrString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding) } documentAttributes:nil error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
//Any UI updates
[self.attributedLabel setAttributedText:attributedString];
});
});
}
@catch (NSException *exception) {
DLog(@"Trace exception : %@",exception.description);
}
@finally {
}
并收到Xcode组织者的以下消息:
发布于 2016-05-04 14:35:08
只有在主线程上才能使用HTML (NSHTMLTextDocumentType
)。
NSAttributedString已经将WebKit用于WebKit文档的所有导入(但不是导出)。因为WebKit文档加载不是线程安全,所以在后台线程上使用它并不安全。 HTML导入程序不应该从后台线程调用(也就是说,选项字典包括值为NSDocumentTypeDocumentAttribute的NSHTMLTextDocumentType)。
因此,尝试在主队列中运行代码,而不是在全局队列中运行。
希望这能有所帮助。
https://stackoverflow.com/questions/37029891
复制相似问题