I am trying to safely read/write data from multiple threads, like described here: Create thread safe array in Swift
Here's a snippet how I read data
private let annotationsQueue = DispatchQueue(label: "myCustoLabel", attributes: .concurrent)
private var unsafeAnnotations = [MapAnnotation]()
private var annotations: [MapAnnotation] {
var annotationsCopy: [MapAnnotation]!
annotationsQueue.sync {
annotationsCopy = self.unsafeAnnotations
}
return annotationsCopy
}
My issue is that annotations
is sometimes being called from DispatchQueue.main.async
, which causes dead lock.
Here's are screenshot of stack trace, when I reach dead lock
My question is how should I handle such situation? Should I somehow force my annotationsQueue
run on background thread?
Or I should write my code, so annotations
are never called from DispatchQueue.main.async
?