Просмотр исходного кода
FetchGlucoseManager heartbeat timer from 1 minute to 1 second
This is a workaround to make the BT heartbeat work on time.
Ideally, a function should be defined here : https://github.com/JohanDegraeve/freeaps-avouspierre/blob/82de85cf6e7138db09924308050cb558ee514feb/FreeAPS/Sources/APS/CGM/HeartBeatManager.swift#L59
This function is called whenever the BT device connects/disconnects or sends data.
With Dex, this would mean there would be several calls to this function, because Dex typically connects, sends several data packets, and finally disconnects, and even after that, continues to connect/disconnect for a few times. (About 10 seconds, then Dex goes to sleep for 5 minutes)
By the last disconnect, xDrip4iOS would already have had the chance to write a new reading in the shared user defaults
But, as the code is rather complex, I could not find which function to add in that heartbeat closure. That's why it's just empty '{}'
So the workaround is to make use of a timer that Ivan implemented. See here https://github.com/JohanDegraeve/freeaps-avouspierre/blob/82de85cf6e7138db09924308050cb558ee514feb/FreeAPS/Sources/APS/FetchGlucoseManager.swift#L19
This timer triggers a new BG reading fetch, originally every minute, with this change here, every second.
A timer is also not ideally. Actually Apple says timers should be canceled when the app goes to the background. Ideally, as described above, the heartbeat function should trigger the BG reading fetch.
But, as Ivan used a timer here, here's what happens
- the timer is created the moment the app launches
- timer expires every interval (originally 1 minute, now 1 second)
- every interval a BG Reading fetch is triggered
When the user brings the app to the background :
- iOS will suspend the app within a few seconds, means the app goes in sleep mode
- the timer will not expire anymore
- As soon as there's BT activity, iOS wakes up the app
- now the timer will continue to do it's work again, which is expire and run the BG reading fetch.
But, in the setup where xDrip4iOS is used, it's actually xDrip4iOS that decodes the data received from Dexcom.
When Dexcom connects, both xDrip4iOS and FAX are woken up by iOS.
xDrip4iOS needs a few seconds to receive the new reading
The timer at FAX expires immediately, but the new reading is not immediately available.
If it's to expire only a minute later, then it will not have the latest reading.
By setting it to 1 second interval, the timer will have the chance to expire a few times (about 5 - 10 times), before iOS brings back the app to sleep.
But during those 5-10 seconds (actually 2 is probably enough), xDrip4iOS has written a new reading to shared defaults, and FAX has the chance to get the latest reading.
As said, this is a workaround.
(cherry picked from commit 8ad09e3637a40dc7c3e84a0d9003b93652083bf7)