이 코드는 CFAttributedString이 스레드로부터 안전하지 않다는 것을 보여 줍니까? 아니면 설정에 문제가 있습니까?CFAttributedString 스레드 안전성
CFAttributedString이 여러 스레드에서 읽는 것이 안전하다고 생각했지만 몇 번 실행될 때마다이 코드에서 충돌이 발생합니다.
@IBAction func testIt(_ sender: Any?) {
let testString = "Hello world! Lets make this a bit longerrrrrrrrrrrrrrrr." as CFString
let testStringLength = CFStringGetLength(testString)
let testAttributedString = CFAttributedStringCreateMutable(kCFAllocatorDefault, testStringLength)
CFAttributedStringReplaceString(testAttributedString, CFRange(location: 0, length: 0), testString)
CFAttributedStringEndEditing(testAttributedString)
for i in 0..<testStringLength {
let range = CFRange(location: i, length: 1)
let keyAndValue = "\(i)" as CFString
CFAttributedStringSetAttribute(testAttributedString, range, keyAndValue, keyAndValue)
}
let immutableTestAttributedString = CFAttributedStringCreateCopy(kCFAllocatorDefault, testAttributedString)
DispatchQueue.concurrentPerform(iterations: 100) { _ in
var index: CFIndex = 0
var effectiveRange: CFRange = CFRange(location: 0, length: 0)
while index < testStringLength {
// Crash happens here EXC_BAD_ACCESS (code=1, address=0x24)
let _ = CFAttributedStringGetAttributes(immutableTestAttributedString, index, &effectiveRange)
index = effectiveRange.location + effectiveRange.length
}
}
}
팁을 보내 주셔서 감사합니다. 불행히도 그 방법으로 문자열을 만들면 같은 위치에 오류가 표시됩니다. 이 경우 CFAttributedString을 사용하는 이유는 코드가 뜨거워서 CFAttributedStringSetAttribute가 훨씬 빠르게 실행되어 NSMutableAttributedString 속성을 추가하는 것 때문입니다. –
신속한 브리징이 작동하는 방법에 대해 더 자세히 알 수있는 지침이 있습니까? 특히 성능 문제는주의해야합니다. Swift의 Core Text로 많은 작업을하고 있으며 현재 최적화 단계에 있습니다. 감사. –
감사합니다. 도움을 받으십시오. NSAttributedString이 안전하고 Core Foundation이 "일반적으로"안전하다고 여기기 때문에 스레드 안전을 기대하고있었습니다. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html 그러나이 경우 NOT, 매우 잘 알고 당신의 도움에 다시 한번 감사드립니다. –