Skip to content

Commit

Permalink
Fix status update from background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai8804858 committed Aug 1, 2024
1 parent c24ae3d commit 7c90a26
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/PathMonitor/PathMonitorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ protocol PathMonitorType: Sendable {
#endif
var path: PathType { get }

func onPathUpdate(_ callback: @escaping (PathType) -> Void)
func onPathUpdate(_ callback: @escaping @Sendable (PathType) -> Void)
func start(queue: DispatchQueue)
func cancel()
}
Expand Down
12 changes: 7 additions & 5 deletions Sources/Reachability/Reachability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ public final class Reachability: Sendable, ObservableObject {
extension Reachability {
private func observeNetworkPathChanges() {
monitor.start(queue: DispatchQueue.global(qos: .utility))
monitor.onPathUpdate { [weak self] path in
guard let self else { return }
status = monitor.connectionStatus(for: path)
isExpensive = path.isExpensive
isConstrained = path.isConstrained
monitor.onPathUpdate { path in
DispatchQueue.main.async { [weak self] in
guard let self else { return }
status = monitor.connectionStatus(for: path)
isExpensive = path.isExpensive
isConstrained = path.isConstrained
}
}
}
}
4 changes: 2 additions & 2 deletions Tests/Mocks/MockPathMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ final class MockPathMonitor: PathMonitorType {
}
#endif

let onPathUpdateCheck = FuncCheck<(PathType) -> Void>()
func onPathUpdate(_ callback: @escaping (PathType) -> Void) {
let onPathUpdateCheck = FuncCheck<@Sendable (PathType) -> Void>()
func onPathUpdate(_ callback: @escaping @Sendable (PathType) -> Void) {
onPathUpdateCheck.call(callback)
}

Expand Down

0 comments on commit 7c90a26

Please sign in to comment.