ios - 在异步队列中快速设置崩溃
问题描述
我在下面的异步队列关闭中使用时发现了奇怪的崩溃Set
,确认它只发生在异步队列中,但Array
有效。
func testA1() {
var set = Set<Int>()
for i in 0...10 {
DispatchQueue.global().async {
set.update(with: i) // Crash here: EXC_BAD_ACCESS
// set.insert(i)
}
}
print(set as Any)
}
func testA2() {
var set = Set<Int>()
for i in 0...10 {
DispatchQueue.global().sync {
set.update(with: i) // Works!
}
}
print(set as Any)
}
func testB() {
var array = [Int]() // Works!
for i in 0...10 {
DispatchQueue.global().async {
array.append(i)
}
}
print(array as Any)
}
斯威夫特版本:
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Target: x86_64-apple-darwin20.2.0
我的错误或快速错误?为什么?
解决方案
Swift 中的大多数数据类型都不是线程安全的。您应该使用不同的方式进行交互。
protocol ThreadSafeExecutor {
var semaphore: DispatchSemaphore { get set }
func wait()
func signal()
}
extension ThreadSafeExecutor {
func wait() {
semaphore.wait()
}
func signal() {
semaphore.signal()
}
}
class YourClass: ThreadSafeExecutor {
func someMethod() {
wait()
defer {
signal()
}
/// thread-safe code
}
}
推荐阅读
- azure-active-directory - 验证用户仍使用 IdentityServer4/Azure AD SSO 在 Azure AD 中进行身份验证
- php - 是否可以引用嵌入在 HTML 文档中的 PHP?
- swift - Xcode Commoncrypto 生成密钥对(私钥和公钥)
- laravel - 安装 laravel 集体不起作用?
- javascript - Appium - 切换到弹出式登录浏览器并返回
- postgresql - 无法通过 pgadmin 3 连接 postgresql 数据库
- amazon-ec2 - 如何从 lamda step 函数运行 ec2 实例中的 shell 脚本?
- linux - mongod:符号查找错误:mongod:未定义符号:_ZN4YAML6detail9node_data12empty_scalarB5cxx11
- java - 在 android 的蓝牙扬声器问题上听不到 twilio 语音通话
- android - 任务 ':app:kaptDebugKotlin' 执行失败。清洁构建时