diff --git a/Sources/Earthquake Network/Models/EQNManager.m b/Sources/Earthquake Network/Models/EQNManager.m index 2a1eb3b..c10051a 100644 --- a/Sources/Earthquake Network/Models/EQNManager.m +++ b/Sources/Earthquake Network/Models/EQNManager.m @@ -61,8 +61,14 @@ - (void)scaricaDatiReteSmartphone { [[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:EQNServerUrlDownloadSmartphoneNetwork] richiesta:EQNTipoChiamataDownloadDati success:^(id result) { - - self.rete_smartphone = [[EQNReteSmartphone alloc] initWithInfo:result]; + + // Parsiamo la risposta (assicurandoci che non contenga valori nulli) + EQNReteSmartphone *rete = [EQNReteSmartphone fromResponse:result]; + if (rete != nil) { + self.rete_smartphone = rete; + } else { + NSLog(@"[EQNManager] Impossibile parsare la risposta di DownloadSmartphoneNetwork"); + } [self performSelectorOnMainThread:@selector(scaricaAreaCheck) withObject:nil waitUntilDone:YES]; } failure:^(NSError * error) { diff --git a/Sources/Earthquake Network/Models/EQNReteSmartphone.swift b/Sources/Earthquake Network/Models/EQNReteSmartphone.swift index d1a86d7..4d29a1f 100644 --- a/Sources/Earthquake Network/Models/EQNReteSmartphone.swift +++ b/Sources/Earthquake Network/Models/EQNReteSmartphone.swift @@ -21,13 +21,27 @@ class EQNReteSmartphone: NSObject { let top10kAvailable: Int let top100kAvailable: Int + // MARK: - Class + + // Sometimes the response returns a broken response, with all values to null. + // In order to avoid crashes due to ObjC-Swift bridging, we need to take a generic nullable object, + // and then cast to the expected type (a dictionary). + @objc static func from(response: Any?) -> EQNReteSmartphone? { + if let info = response as? [[String: Any]?] { + return .init(info: info) + } + return nil + } + // MARK: - Init - @objc init(info: [[String: Any]]) { + private init(info: [[String: Any]?]) { // merge array in a single dictionary - let allValues = info.reduce([:]) { (result, dictionary) -> [String: Any] in - return result.merging(dictionary, uniquingKeysWith: { (_, new) in new }) - } + let allValues = info + .compactMap { $0 } + .reduce([:]) { (result, dictionary) -> [String: Any] in + return result.merging(dictionary, uniquingKeysWith: { (_, new) in new }) + } self.counterLastDayAlerts = allValues.integer(forKey: "eq", orDefault: 0) self.counterTotalAlerts = allValues.integer(forKey: "eq_p", orDefault: 0)