I have a class that reads from and writes to a dictionary using a serial dispatch queue. While the write operations are asynchronous, the read operations are synchronous. I want to avoid the use of a completion handler for reading. Does this create any issues? The issue could range from deadlocks or crashes by calling from the main thread.
public final class UserRegistry {
private var infoMap: [String: User] = [:]
private let queue: DispatchQueue = .init(label: "userRegistry.queue")
func register(_ user: User) {
queue.async { [weak self] in
self?.infoMap[user.id] = user
}
}
func deregister(_ user: User) {
queue.async { [weak self] in
self?.infoMap[user.id] = nil
}
}
func getUser(for id: String) -> User? {
queue.sync {
infoMap[id]
}
}
}
I tried to create a completion handler and that would cause a series of changes at the call site across the code base. I expect that reading synchrnously should be safe regardless of which thread the consumer calls this from.
Read operation - func getUser(for id: String) -> User?
Write operations - func register(_ user: User)
& func deregister(_ user: User)