即使 iOS 文档说:
NLayoutManager、NSTextStorage 和 NSTextContainer 可以从子线程访问,只要应用程序保证从单个线程访问即可。
我偶尔会遇到这个异常:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“仅在主线程上运行!”
这是回溯:
Exception Type: SIGABRT
Exception Codes: #0 at 0x197bca58c
Crashed Thread: 7
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
Last Exception Backtrace:
0 CoreFoundation 0x000000018afd2f50 __exceptionPreprocess + 132
1 libobjc.A.dylib 0x00000001974dc1fc objc_exception_throw + 56
2 CoreFoundation 0x000000018afd2e10 +[NSException raise:format:arguments:] + 112
3 Foundation 0x000000018bb0ae20 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 84
4 UIFoundation 0x00000001940f0654 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 412
5 UIFoundation 0x00000001940f0318 -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 1748
6 UIFoundation 0x000000019411ec2c _enableTextViewResizing + 236
7 UIFoundation 0x0000000194123e18 -[NSLayoutManager textContainerForGlyphAtIndex:effectiveRange:] + 484
8 UIFoundation 0x0000000194125c60 -[NSLayoutManager glyphRangeForTextContainer:] + 352
需要注意的一件事是 myNLayoutManager
附加到UITextView
. 您可以将其视为UITextView
用于在后台线程中进行文本布局的普通方法。
那么这是否意味着与文档所说的相矛盾,NLayoutManager
如果它附加到单个后台线程,则不能安全地使用它UITextView
?