From c580416e6249d0cfd3d53ef61fda2e6dae1df40d Mon Sep 17 00:00:00 2001 From: Johan Degraeve Date: Sun, 12 Jun 2022 23:31:50 +0200 Subject: [PATCH] fix bug, no upload new readings after Internet failure The bug was that after restore of failed Internet connection, new readings are not uploaded to NS. - as mentioned in the comments of func uploadData(dataToUpload: Any, httpMethod: String?, path: String, completionHandler: (() -> ())?) completionhandler should be called only when upload was successful. But the result of the upload was never checked. So probably, in some error cases, completionhandler was called with success. As a result, the upload was considered as successful and not redone when internet comes back - also some updates in func uploadDataAndGetResponse. Set nightScoutResult to .failed when there was an error. --- .../NightScout/NightScoutUploadManager.swift | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/xdrip/Managers/NightScout/NightScoutUploadManager.swift b/xdrip/Managers/NightScout/NightScoutUploadManager.swift index 981b2a1c..87aa3868 100644 --- a/xdrip/Managers/NightScout/NightScoutUploadManager.swift +++ b/xdrip/Managers/NightScout/NightScoutUploadManager.swift @@ -1047,7 +1047,8 @@ public class NightScoutUploadManager: NSObject { uploadDataAndGetResponse(dataToUpload: dataToUpload, httpMethod: httpMethod, path: path) { _, nightScoutResult in - if let completionHandler = completionHandler { + // completion handler to be called only if upload as successful + if let completionHandler = completionHandler, nightScoutResult.successFull() { completionHandler() @@ -1139,6 +1140,9 @@ public class NightScoutUploadManager: NSObject { // error cases if let error = error { trace(" failed to upload, error = %{public}@", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .error, error.localizedDescription) + + nightScoutResult = NightScoutResult.failed + return } @@ -1178,18 +1182,26 @@ public class NightScoutUploadManager: NSObject { } } catch { - // json decode fails, upload will be considered as failed + // json decode fails, upload will be considered as failed + nightScoutResult = NightScoutResult.failed + return } } trace(" failed to upload, statuscode = %{public}@", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .error, response.statusCode.description) + nightScoutResult = NightScoutResult.failed + return } } else { trace(" response is not HTTPURLResponse", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .error) + + nightScoutResult = NightScoutResult.failed + + return } // successful cases @@ -1220,6 +1232,8 @@ public class NightScoutUploadManager: NSObject { trace(" error : %{public}@", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .info, error.localizedDescription) + completionHandler(nil, NightScoutResult.failed) + } }