如果不在主线程上,NSAttributedString initWithData和NSHTMLTextDocumentType会崩溃 [英] NSAttributedString initWithData and NSHTMLTextDocumentType crash if not on main thread

查看:1821
本文介绍了如果不在主线程上,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

当我没有指定属性低于其工作时

when i don't specify the attribute bellow, than it works

NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,

我需要在后台线程上解析字符串,但这使得它不可能:(

I need to parse the string on background thread, but this makes it impossible :(

推荐答案

文档非常清楚。


不应从后台线程调用HTML导入程序(即,选项字典包含NSDocumentTypeDocumentAttribute,其值为NSHTMLTextDocumentType)。它将尝试与主线程同步,失败和超时。从th调用它主线程工作(但如果HTML包含对外部资源的引用,仍然可以超时,应该不惜一切代价避免)。 HTML导入机制用于实现降价(即文本样式,颜色等),而不是一般的HTML导入。

The HTML importer should not be called from a background thread (that is, the options dictionary includes NSDocumentTypeDocumentAttribute with a value of NSHTMLTextDocumentType). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.

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

Using the HTML importer (NSHTMLTextDocumentType) is only possible on the main thread.

(来源: Apple的文档

这篇关于如果不在主线程上,NSAttributedString initWithData和NSHTMLTextDocumentType会崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆