Background user notifications from observer query completion handler

I have observer queries for certain hksample types which in turn run sample queries to obtain data. i want to fire notifications from the completion handler, for certain data thresholds (like steps taken) from these queries. is this the right approach, say for % step goals completed or should i consider background app refresh (background modes)?


Note: either way i will need the iphone to be unlocked as healthkit database cannot be queried when the phone is locked.

if say after a reboot my app is not launched, can the system launch the app in background as requested by background app refresh's

    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

time interval?


My decision will be based on whether my app can be launched automatically by the system for the first time. i'm a bit confused about this.


if it can be then backgroundmode can be useful otherwise the app launching during observer query will do the job.

You'll want to look into Background Delivery (https://developer.apple.com/documentation/healthkit/hkhealthstore/1614175-enablebackgrounddelivery). This will allow HealthKit to launch your application in the background when data that you're interested in is saved. You'll use that in combination with HKObserverQuery and, usually, HKAnchoredObjectQuery or HKStatisticsQuery/HKStatisticsCollectionQuery.

On a side note, while HKSampleQuery is certainly simple and capable, we encourage you to look closely at HKStatisticsQuery or HKStatisticsCollectionQuery for these sorts of tasks first and only fall back to HKSampleQuery if you really do need access to every individual sample. The statistics queries provide many benefits, such as performing correct de-duplication between samples of from difference devices and better performance when you're only interested in summing up data.
Background user notifications from observer query completion handler
 
 
Q