Every now and then, my app crashes with a EXC_BAD_ACCESS when accessing a Dictionary, although the object exists and has been accessed many times before the crash without any problem.
It is created at start of app with:
static var whiteLabelling = [String : String]()
and is never recreated afterwards.
Entries may be added, changed and removed at runtime, and, depending on the situation, it can run through a FareEngine.whiteLabelling.removeAll()
Multiple threads access it simultaneously as well without crashing.
You should ensure that you always read from the same thread.
You could use always main thread with a basic DispatchQueue.main.async{}
. That way, it's always in the same queue (main thread/thread 0).
Another possible way would be using a private queue, ensuring it's always in that same queue.
FareEngine
static private var whiteLabellingQueue = DispatchQueue(label: "com.yourApp.whiteLabellingQueue, qos: .userInitiated) //or any other `qos`
whiteLabelling
private (to ensure there is no more issue with other code accessing it):private static var whiteLabelling: [String: String]
func setWhiteLabelling(value: String, forKey key: String) {
whiteLabellingQueue.sync {
self.whiteLabelling[key] = value
}
}
func getWhiteLabelling(forKey key: String) -> String? {
let value: String?
whiteLabellingQueue.sync {
value = self.whiteLabelling[key]
}
return value
}
Now, when you want to access the values:
let req = FareEngine.getWhiteLabelling(forKey: "GPSACCURACY")
Since, you can have a remove all, do the same:
func cleanWhiteLabelling() {
whiteLabellingQueue.sync {
self.whiteLabelling.removeAll()
}
}