Skip to content

Commit b5c2869

Browse files
committed
Attempt to fix crash by replacing checked continuation with AsyncStream and returning the first value.
1 parent 379d836 commit b5c2869

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

WooCommerce/Classes/Tools/BackgroundTasks/BackgroundTaskRefreshDispatcher.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,27 @@ private struct BackgroundTaskSystemInfo {
173173
}
174174

175175
private static func getNetworkInfo() async -> NetworkInfo {
176-
return await withCheckedContinuation { continuation in
177-
let monitor = NWPathMonitor()
178-
var hasResumed = false
179-
180-
monitor.pathUpdateHandler = { path in
181-
guard !hasResumed else { return }
182-
hasResumed = true
183-
monitor.cancel()
184-
continuation.resume(returning: NetworkInfo(path: path))
185-
}
176+
let monitor = NWPathMonitor()
177+
let queue = DispatchQueue(label: "network.monitor.queue")
178+
179+
let (stream, continuation) = AsyncStream.makeStream(of: NWPath.self)
180+
181+
monitor.pathUpdateHandler = { path in
182+
continuation.yield(path)
183+
}
186184

187-
let queue = DispatchQueue(label: "network.monitor.queue")
188-
monitor.start(queue: queue)
185+
monitor.start(queue: queue)
186+
187+
defer {
188+
continuation.finish()
189+
monitor.cancel()
190+
}
191+
192+
if let path = await stream.first(where: { _ in true }) {
193+
return NetworkInfo(path: path)
194+
} else {
195+
// Fallback in case no path is received.
196+
return NetworkInfo(type: "unknown", isExpensive: false, isLowDataMode: false)
189197
}
190198
}
191199
}

0 commit comments

Comments
 (0)