NSAttributedString initWithData和NSHTMLTextDocumentType在主线程中不会崩溃

调用

NSAttributedString * as = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; 

在主线程以外,导致崩溃

 1 0x194b861fc <redacted> 2 0x19801d31c <redacted> 3 0x198010eb4 _os_once 4 0x19801aa2c pthread_once 5 0x195a0858c <redacted> 6 0x195a07c78 WebKitInitialize 7 0x18bb38918 <redacted> 8 0x10020cdf0 _dispatch_client_callout 9 0x10020dcfc dispatch_once_f 10 0x1977f8bd0 <redacted> 11 0x1978009ac <redacted> 12 0x19780bdb8 <redacted> 13 0x1940b259c _NSReadAttributedStringFromURLOrData 14 0x1940b0eb4 <redacted> 15 0x1001041a0 -[MPMPostTextBrickCell attributedTextWithHTMLString:] 16 0x100103d3c __39-[MPMPostTextBrickCell setupWithBrick:]_block_invoke 17 0x187fb3508 <redacted> 18 0x187f04c94 <redacted> 19 0x187ef461c <redacted> 20 0x187fb626c <redacted> 21 0x10020cdf0 _dispatch_client_callout 22 0x100217854 _dispatch_queue_drain 23 0x100210120 _dispatch_queue_invoke 24 0x10021975c _dispatch_root_queue_drain 25 0x10021af18 _dispatch_worker_thread3 26 0x1980192e4 _pthread_wqthread 27 0x198018fa8 start_wqthread 

当我不指定属性波纹pipe,比它的作品

 NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 

我需要parsing背景线程上的string,但这使得它不可能:(

文件是相当明确的。

不应该从后台线程调用HTML导入器(也就是说,选项字典包含NSDocumentTypeDocumentAttribute,其值为NSHTMLTextDocumentType)。 它会尝试与主线程同步,失败并超时。 从主线程调用它(但是如果HTML包含对外部资源的引用,仍然可以超时,这应该不惜一切代价地避免)。 HTML导入机制是为了实现像markdown(即文本样式,颜色等),而不是一般的HTML导入。

使用HTML导入器( NSHTMLTextDocumentType )只能在主线程中使用。

(来源: 苹果文档 )

也许回答这个问题来得太晚,但可能会帮助别人。

实际上, NSAttributedStringNSHTMLTextDocumentType必须在主队列或全局队列上asynchronous运行。

您可以在后台线程中使用它,但是您必须在全局或主队列中分派包含NSAttributedString初始值设定项的代码块。 例如:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in print("Started on \(NSThread.currentThread())") let encodedData = "<font color=\"red\">Hello</font>".dataUsingEncoding(NSUTF8StringEncoding)! let attributedOptions : [String: AnyObject] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding ] let attributedString = (try? NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) ?? NSAttributedString(string: "")) print("Finished") }