Fix async call not waiting for results

This commit is contained in:
ronaldheft
2022-08-23 18:22:11 -04:00
parent 099be648bf
commit d5f39e5cb1
+10 -6
View File
@@ -32,7 +32,7 @@ class PlayerProgress {
public func syncToServer() async { public func syncToServer() async {
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncToServer") let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncToServer")
updateAllServerSessionFromLocalSession() await updateAllServerSessionFromLocalSession()
await UIApplication.shared.endBackgroundTask(backgroundToken) await UIApplication.shared.endBackgroundTask(backgroundToken)
} }
@@ -87,11 +87,15 @@ class PlayerProgress {
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil) NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil)
} }
private func updateAllServerSessionFromLocalSession() { private func updateAllServerSessionFromLocalSession() async {
let sessions = try! Realm().objects(PlaybackSession.self).where({ $0.serverConnectionConfigId == Store.serverConfig?.id }) await withTaskGroup(of: Void.self) { [self] group in
for session in sessions { for session in try! await Realm().objects(PlaybackSession.self).where({ $0.serverConnectionConfigId == Store.serverConfig?.id }) {
let session = session.freeze() let session = session.freeze()
Task { await updateServerSessionFromLocalSession(session) } group.addTask {
await self.updateServerSessionFromLocalSession(session)
}
}
await group.waitForAll()
} }
} }