Hi,
Wondering if anyone can help me with this question. I have a lot of async APIs called from various parts of my application. They all manage login tokens and follow this kind of pattern:
func syncData() async -> Void {
do {
let timezone = TimeZone.current.secondsFromGMT()
let timezoneInHours = timezone / 60 / 60
if let data = try await APIHelper.authCall(data: [ "timezone" : timezoneInHours ], method: "POST", url: "v1/retrieve") {
let decoder = JSONDecoder()
let jsonData = try decoder.decode(ResponseObject.self, from: data)
if let tokenData = jsonData.token {
// Processing
}
}
catch(let error) {
print(error)
}
}
the APIHelper.authCall()
is just a wrapper for firing APIs with some additional data sent automatically in the header etc.
The problem I am now facing is that there appears to be overlap between different APIs as they can fire all at once if the users state changes a lot...
.onChange(of: data2) { oldValue, newValue in
Task {
await callAPI()
}
}
.onChange(of: data1) { oldValue, newValue in
Task {
await callAPI()
}
}
.onAppear {
Task {
await callAPI()
}
}
It seems the await APIs work a bit like other languages where multiple APIs can be fired off at the same time, but the response may appear later and occasionally in a different order. Since some of these handle login state, it can sometimes lead to users being assigned an old token and being logged out.
To solve this, I would like each API to be added to a global queue which is accessible from anywhere in the application, and waits for a ResponseObject
type before firing the next API in the queue. hat way I can ensure that the login token is always correct and up to date. I was trying to accomplish this through a dispatch queue but didn't have much luck with await/async functions. I also tried with actors but faced a couple of problems.
Is there a best practice or standard way to do this? It seems like it would be quite a common problem - but I haven't found a standard process for doing it in Swift - is the answer using actors?