Commit 0edd3bff by Demid Merzlyakov

Health and Notifications are not immediately re-requested on error anymore.

parent 4d5ebef7
...@@ -214,26 +214,26 @@ public class LocationManager { ...@@ -214,26 +214,26 @@ public class LocationManager {
log.info("Update health for: \(location)") log.info("Update health for: \(location)")
healthSource.updateHelath(for: location) { [weak self] (health, error) in healthSource.updateHelath(for: location) { [weak self] (health, error) in
guard let self = self else { return } guard let self = self else { return }
guard let health = health else {
var updatedHealth = health
if health == nil {
if let error = error { if let error = error {
self.log.error("Update health (\(location)) error: \(error)") self.log.error("Update health (\(location)) error: \(error)")
} }
else { else {
self.log.error("Update health (\(location)) error: unknown error") self.log.error("Update health (\(location)) error: unknown error")
} }
return updatedHealth = Health(lastUpdateTime: Date(), airQuality: nil, pollutants: [String : Pollutant]())
// TODO: we need to somehow track failed attempts, so that we didn't request again and again in case of an error (e.g. server is down).
} }
DispatchQueue.main.async { else {
if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) { self.log.info("Update health (\(location)): success")
self.locations[indexToUpdate].health = health }
}
else if self.defaultLocation == location { self.makeChanges(to: location, in: "health") { (location) -> Location in
self.defaultLocation.health = health var updatedLocation = location
} updatedLocation.health = updatedHealth
else { return updatedLocation
self.log.warning("Update health: Failed to find location after update. Maybe it was deleted while the update was performed. Maybe something went wrong. Location: \(location)")
}
} }
} }
} }
...@@ -248,27 +248,24 @@ public class LocationManager { ...@@ -248,27 +248,24 @@ public class LocationManager {
log.info("Update notifications for \(location)") log.info("Update notifications for \(location)")
nwsAlertsManager.fetchActiveAlerts(for: location) { [weak self] (alerts, error) in nwsAlertsManager.fetchActiveAlerts(for: location) { [weak self] (alerts, error) in
guard let self = self else { return } guard let self = self else { return }
guard let alerts = alerts else { var updatedNotifications = Notifications(updatedAt: Date(), nwsAlerts: [NWSAlert]()) // in case of error
if let alerts = alerts {
self.log.info("Update notifications (\(location)): success.")
updatedNotifications = Notifications(updatedAt: Date(), nwsAlerts: alerts)
}
else {
if let error = error { if let error = error {
self.log.error("Update notifications (\(location)) error: \(error)") self.log.error("Update notifications (\(location)) error: \(error)")
} }
else { else {
self.log.error("Update notifications (\(location)) error: unknown error") self.log.error("Update notifications (\(location)) error: unknown error")
} }
return
} }
let updatedNotifications = Notifications(updatedAt: Date(), nwsAlers: alerts)
self.log.info("Update notifications (\(location)): success.") self.makeChanges(to: location, in: "notifications") { (location) -> Location in
DispatchQueue.main.async { var updatedLocation = location
if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) { updatedLocation.notifications = updatedNotifications
self.locations[indexToUpdate].notifications = updatedNotifications return updatedLocation
}
else if self.defaultLocation == location {
self.defaultLocation.notifications = updatedNotifications
}
else {
self.log.warning("Update notifications: Failed to find location after update. Maybe it was deleted while the update was performed. Maybe something went wrong. Location: \(location)")
}
} }
} }
} }
...@@ -289,27 +286,33 @@ public class LocationManager { ...@@ -289,27 +286,33 @@ public class LocationManager {
weatherUpdateSource.updateWeather(for: location, type: updateType) { [weak self] (updatedLocation, error) in weatherUpdateSource.updateWeather(for: location, type: updateType) { [weak self] (updatedLocation, error) in
guard let self = self else { return } guard let self = self else { return }
guard let updatedLocation = updatedLocation else { if let updatedLocation = updatedLocation {
self.log.info("Update weather finished for \(location)")
self.makeChanges(to: location, in: "weather") { (oldLocation) -> Location in
oldLocation.mergedWith(incrementalChanges: updatedLocation)
}
}
else {
if let error = error { if let error = error {
self.log.error("Update weather (\(location)) error: \(error)") self.log.error("Update weather (\(location)) error: \(error)")
} }
else { else {
self.log.error("Update weather (\(location)) error: unknown error") self.log.error("Update weather (\(location)) error: unknown error")
} }
return
} }
}
DispatchQueue.main.async { }
self.log.info("Update weather finished for \(location)")
if let indexToUpdate = self.locations.firstIndex(where: { $0 == updatedLocation }) { private func makeChanges(to location: Location, in operation: String, changes: @escaping (Location) -> Location) {
self.locations[indexToUpdate] = self.locations[indexToUpdate].mergedWith(incrementalChanges: updatedLocation) DispatchQueue.main.async {
} if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) {
else if self.defaultLocation == updatedLocation { self.locations[indexToUpdate] = changes(self.locations[indexToUpdate])
self.defaultLocation = self.defaultLocation.mergedWith(incrementalChanges: updatedLocation) }
} else if self.defaultLocation == location {
else { self.defaultLocation = changes(self.defaultLocation)
self.log.warning("Update weather: Failed to find location after update. Maybe it was deleted while the update was performed. Maybe something went wrong. Location: \(updatedLocation)") }
} else {
self.log.warning("Failed to find location after update (\(operation)). Maybe it was deleted while the update was performed. Maybe something went wrong. Location: \(location)")
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment