今天遇到原本没问题的项目在播放音频后报错:
Simultaneous accesses to 0x28261ebd0, but modification requires exclusive access. Previous access (a

原因是结构体需要同时进行读写操作导致线程不安全
代码如下:
sound.requestSound(success: { (url) in
self.startPlayRecord(path: url)
sound.isPlaying = true // 1
tempChat.type = .sound(sound) // 2
}, failure: {
ChatManager.shared.chats[index] = tempChat
})
修改后
var mySound = sound
var myTempChat = tempChat
sound.requestSound(success: { (url) in
self.startPlayRecord(path: url)
mySound.isPlaying = true // 1
myTempChat.type = .sound(mySound) // 2
}, failure: {
ChatManager.shared.chats[index] = myTempChat
})
网友评论