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 {
log.info("Update health for: \(location)")
healthSource.updateHelath(for: location) { [weak self] (health, error) in
guard let self = self else { return }
guard let health = health else {
var updatedHealth = health
if health == nil {
if let error = error {
self.log.error("Update health (\(location)) error: \(error)")
}
else {
self.log.error("Update health (\(location)) error: unknown error")
}
return
// 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 {
if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) {
self.locations[indexToUpdate].health = health
}
else if self.defaultLocation == location {
self.defaultLocation.health = health
updatedHealth = Health(lastUpdateTime: Date(), airQuality: nil, pollutants: [String : Pollutant]())
}
else {
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)")
self.log.info("Update health (\(location)): success")
}
self.makeChanges(to: location, in: "health") { (location) -> Location in
var updatedLocation = location
updatedLocation.health = updatedHealth
return updatedLocation
}
}
}
......@@ -248,27 +248,24 @@ public class LocationManager {
log.info("Update notifications for \(location)")
nwsAlertsManager.fetchActiveAlerts(for: location) { [weak self] (alerts, error) in
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 {
self.log.error("Update notifications (\(location)) error: \(error)")
}
else {
self.log.error("Update notifications (\(location)) error: unknown error")
}
return
}
let updatedNotifications = Notifications(updatedAt: Date(), nwsAlers: alerts)
self.log.info("Update notifications (\(location)): success.")
DispatchQueue.main.async {
if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) {
self.locations[indexToUpdate].notifications = updatedNotifications
}
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)")
}
self.makeChanges(to: location, in: "notifications") { (location) -> Location in
var updatedLocation = location
updatedLocation.notifications = updatedNotifications
return updatedLocation
}
}
}
......@@ -289,27 +286,33 @@ public class LocationManager {
weatherUpdateSource.updateWeather(for: location, type: updateType) { [weak self] (updatedLocation, error) in
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 {
self.log.error("Update weather (\(location)) error: \(error)")
}
else {
self.log.error("Update weather (\(location)) error: unknown error")
}
return
}
}
}
private func makeChanges(to location: Location, in operation: String, changes: @escaping (Location) -> Location) {
DispatchQueue.main.async {
self.log.info("Update weather finished for \(location)")
if let indexToUpdate = self.locations.firstIndex(where: { $0 == updatedLocation }) {
self.locations[indexToUpdate] = self.locations[indexToUpdate].mergedWith(incrementalChanges: updatedLocation)
if let indexToUpdate = self.locations.firstIndex(where: { $0 == location }) {
self.locations[indexToUpdate] = changes(self.locations[indexToUpdate])
}
else if self.defaultLocation == updatedLocation {
self.defaultLocation = self.defaultLocation.mergedWith(incrementalChanges: updatedLocation)
else if self.defaultLocation == location {
self.defaultLocation = changes(self.defaultLocation)
}
else {
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)")
}
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