Mitigate a possible race condition in the first few calls to getUser() for our own userId
The Session.getUser()
function is synchronous when in reality it should probably be async and concurrency controlled to prevent race conditions.
Unfortunately we need it to be sync so we can call it from SwiftUI Views.
This MR mitigates the problem by pre-populating the users
dictionary with a User
for our own account during Session.init()
when there can be no other concurrent access. This should fix the problem where, soon after session startup, multiple callers call into getUser()
at the same time, trying to get our own user. Currently this can cause the app to crash.
Note: If this doesn't fix the crashes, it may be that sometimes callers of getUser()
are requesting other users at the same time. In that case, we can probably reduce the problem again by having the sync()
loop pre-populate User
s in the dictionary for any userId that has sent a recent message. Then when the SwiftUI Views go to query for those users, they already exist and getUser()
doesn't have to modify the dictionary.