I have NetworkProvider which will make api call continuously and also once I received the data i will update the userid. At the same time I will access the userid from other functions.
This is data race condition, can someone help to remove the condition.
`
class NetworkProvider {
public var userID: String
func observeStateChange() {
FIRAuth.auth()?.addStateDidChangeListener({ (auth, authenticatedUser) in
if let user = authenticatedUser {
userID = user.uid
}
}
}
func currentUserID() -> String {
return self.userID
}
}`
As different threads use the same value of i, the data-races occur. One way to fix the problem is to pass i to work() by value. This ensures that each thread has its own private copy of i with a unique value.
A common mechanism to avoid data races is to force a mutual exclusion. In the previous example, you can enforce sequence 1 by: Locking a mutex before Task1: sharedVar1 = 11; Unlocking the mutex after Task1: do_sth_wth_shared_resources1();
A data race occurs when 2 or more threads trying to access (read/write) the same memory location asynchronously at the same time. In the context of Swift, it usually happens when we try to modify an object's state using a dispatch queue.
A race condition occurs when the timing or order of events affects the correctness of a piece of code. A data race occurs when one thread accesses a mutable object while another thread is writing to it.
Use DispatchQueue can avoid data race:
class NetworkProvider {
let isolationQueue = DispatchQueue(label: "com.your.domain.xxx", attributes: .concurrent)
private var _userID: String
public var userID: String {
set { isolationQueue.async(flags: .barrier) { self._userID = newValue } }
get { return isolationQueue.sync { _userID } }
}
func observeStateChange() {
FIRAuth.auth()?.addStateDidChangeListener({ (auth, authenticatedUser) in
if let user = authenticatedUser {
userID = user.uid
}
}
}
func currentUserID() -> String {
return self.userID
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With