fix: Rework init to avoid crash when server returns null values
This commit is contained in:
@@ -61,8 +61,14 @@
|
|||||||
- (void)scaricaDatiReteSmartphone
|
- (void)scaricaDatiReteSmartphone
|
||||||
{
|
{
|
||||||
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:EQNServerUrlDownloadSmartphoneNetwork] richiesta:EQNTipoChiamataDownloadDati success:^(id result) {
|
[[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];
|
[self performSelectorOnMainThread:@selector(scaricaAreaCheck) withObject:nil waitUntilDone:YES];
|
||||||
} failure:^(NSError * error) {
|
} failure:^(NSError * error) {
|
||||||
|
|||||||
@@ -21,13 +21,27 @@ class EQNReteSmartphone: NSObject {
|
|||||||
let top10kAvailable: Int
|
let top10kAvailable: Int
|
||||||
let top100kAvailable: 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
|
// MARK: - Init
|
||||||
|
|
||||||
@objc init(info: [[String: Any]]) {
|
private init(info: [[String: Any]?]) {
|
||||||
// merge array in a single dictionary
|
// merge array in a single dictionary
|
||||||
let allValues = info.reduce([:]) { (result, dictionary) -> [String: Any] in
|
let allValues = info
|
||||||
return result.merging(dictionary, uniquingKeysWith: { (_, new) in new })
|
.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.counterLastDayAlerts = allValues.integer(forKey: "eq", orDefault: 0)
|
||||||
self.counterTotalAlerts = allValues.integer(forKey: "eq_p", orDefault: 0)
|
self.counterTotalAlerts = allValues.integer(forKey: "eq_p", orDefault: 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user