Check network reachability before attempting to restart the sync service

This commit is contained in:
Stefan Ceriu
2023-10-12 12:39:53 +03:00
parent 8e03b89641
commit 7f09c02298
6 changed files with 70 additions and 14 deletions
@@ -23,6 +23,7 @@ class ClientProxy: ClientProxyProtocol {
private let client: ClientProtocol
private let backgroundTaskService: BackgroundTaskServiceProtocol
private let appSettings: AppSettings
private let networkMonitor: NetworkMonitorProtocol
private var sessionVerificationControllerProxy: SessionVerificationControllerProxy?
private let mediaLoader: MediaLoaderProtocol
@@ -77,10 +78,14 @@ class ClientProxy: ClientProxyProtocol {
loadingStateSubject.asCurrentValuePublisher()
}
init(client: ClientProtocol, backgroundTaskService: BackgroundTaskServiceProtocol, appSettings: AppSettings) async {
init(client: ClientProtocol,
backgroundTaskService: BackgroundTaskServiceProtocol,
appSettings: AppSettings,
networkMonitor: NetworkMonitorProtocol) async {
self.client = client
self.backgroundTaskService = backgroundTaskService
self.appSettings = appSettings
self.networkMonitor = networkMonitor
clientQueue = .init(label: "ClientProxyQueue", attributes: .concurrent)
@@ -94,6 +99,16 @@ class ClientProxy: ClientProxyProtocol {
self?.callbacks.send(.receivedAuthError(isSoftLogout: isSoftLogout))
})
networkMonitor.reachabilityPublisher
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { [weak self] reachability in
if reachability == .reachable {
self?.startSync()
}
}
.store(in: &cancellables)
await configureAppService()
loadUserAvatarURLFromCache()
@@ -136,6 +151,11 @@ class ClientProxy: ClientProxyProtocol {
return
}
guard networkMonitor.reachabilityPublisher.value == .reachable else {
MXLog.warning("Ignoring request, network unreachable.")
return
}
MXLog.info("Starting sync")
Task {