kingstinct/react-native-healthkit

Background observing with this library

Closed this issue · 8 comments

I am making use of activity mappings when the user has our react native app open but I'm wondering if it is possible to sync activities in the background (app in closed or background state) with this library.

Hi @JDMathew and thanks for your question. We do expose the enableBackgroundDelivery method in the library, which I think should be enough to make it work. With that said I'm not sure if anyone has actually tried it.

I would suggest having a quick look at the official Apple Docs and make sure the queries you want to run in the background are not dependent on UI being rendered (i.e. outside the React lifecycle). Also make sure to add the Healthkit background delivery entitlement.

Good luck and let me know how it goes! :)

@JDMathew just to let you know this #20 is part of the new 4.2.0 release.

@robertherber, awesome thanks for the update. I'll check out the docs and see if I can get it working. Might be worth updating the readme to specify this is possible.

ha3 commented

@robertherber hi. I've been transitioning my project from https://github.com/agencyenterprise/react-native-health to this library. First of all, thanks for the amazing work. The API is very intuitive and elegant!

I was trying to enable background delivery. One thing I noticed. In Apple docs it states:

As soon as your app launches, HealthKit calls the update handler for any observer queries that match the newly saved data. If you plan on supporting background delivery, set up all your observer queries in your app delegate’s application(_:didFinishLaunchingWithOptions:) method. By setting up the queries in application(_:didFinishLaunchingWithOptions:), you ensure that you’ve instantiated your queries, and they’re ready to use before HealthKit delivers the updates.

whereas this library's enableBackgroundDelivery seems like it should only be called from the js side.

What approach do you recommend for this?
Thanks.

@ha3 I haven't actually been using the library in the background yet myself. Does it seem to work with the JS implementation or do we need to go fully native on this one?

If not - the option would be to add this as an Expo Config plugin (complemented by manual instruction for Bare React Native projects) that covers what Apple mentions in their docs.

ha3 commented

@robertherber We conducted tests with the current API; and it seems working both on background and terminated states. However the phrasing here ...you ensure that you’ve instantiated your queries, and they’re ready to use before HealthKit delivers the updates... concerned me over possible race conditions—but I am not sure, not very knowledgeable in the native side.

Another thing; now, it is not possible to retrieve only new changes reliably. Apple seems to suggest using HKAnchoredObjectQuery for this.

@ha3 Both are great improvement ideas, I've added them as issues, please feel free to elaborate on them:
#51 #50

mafen commented

@robertherber We conducted tests with the current API; and it seems working both on background and terminated states. However the phrasing here ...you ensure that you’ve instantiated your queries, and they’re ready to use before HealthKit delivers the updates... concerned me over possible race conditions—but I am not sure, not very knowledgeable in the native side.

Another thing; now, it is not possible to retrieve only new changes reliably. Apple seems to suggest using HKAnchoredObjectQuery for this.

How did you conduct tests?