kingstinct / react-native-healthkit

HealthKit bindings for React Native with TypeScript
https://kingstinct.com/react-native-healthkit/
MIT License
239 stars 49 forks source link

HealthKit.subscribeToChanges does not work with RN New Architecture enabled #106

Open KesoGizmoYoshi opened 2 months ago

KesoGizmoYoshi commented 2 months ago

https://github.com/KesoGizmoYoshi/rn-healthkit-test

I am unable to get subscribeToChanges to work on a project running with RN New Architecture enabled. The same code works perfectly when running on a prebuilt version with newArchEnabled set to false. When newArchEnabled is set to false, the console log successfully triggers when a new sample exists. However, when newArchEnabled is set to true, nothing happens, and the subscription does not seem to work.

robertherber commented 2 months ago

Interesting. The example expo app is using the new architecture - but it doesn't really test the background functionality. We haven't upgraded to the new architecture in our own apps yet.

Are other background features working as expected with the new architecture enabled?

KesoGizmoYoshi commented 1 month ago

I did also try useSubscribeToChanges, but with no success with new architecture.

robertherber commented 1 month ago

@KesoGizmoYoshi That makes sense since useSubscribeWithChanges uses subscribeToChanges under the hood.

KesoGizmoYoshi commented 1 month ago

I see! Anything else you want me to test?

robertherber commented 1 month ago

Do you see anything in the device logs that might give a hint on what might be going on?

Also version of React Native and Expo if applicable would be helpful! :) I'll probably dig deeper into this when we have time to test out the new architecture ourselves 👍

KesoGizmoYoshi commented 1 week ago

Sorry for a late response, but I have only tested on expo 51.0.32 and react-native 0.75.2, hopefully I can test soon with expo 52. newArchEnabled: true https://gist.github.com/KesoGizmoYoshi/c796de82f5f1a2b6b1fc714c28485bda

newArchEnabled: false https://gist.github.com/KesoGizmoYoshi/2b3629a026c726b8d91d70ce9eb841e4

dhgavali commented 8 hours ago

I tried using subscribeToChange method as well. But isn't working on my end. Any simple example on how to get the data updates in background would be helpful and appreciated. My project details - "react-native": "0.74.5", "expo": "^51.0.38" "@kingstinct/react-native-healthkit": "^8.2.0",

Here is sample code what I'm tried.

useEffect(() => {
        let unsubscribe :any;
        if (hasRequestedAuthorization) {
            unsubscribe = HealthKit.subscribeToChanges(HKQuantityTypeIdentifier.heartRate, () => {

   const  mostRecentData  = await queryQuantitySamples(HKQuantityTypeIdentifier.heartRate, {
                limit: 1,
              });
console.log("most recent data is", mostRecentData);

          });
        }

        return () => unsubscribe;
      }, [hasRequestedAuthorization]);

I'm calling queryQuantitySample in subscribeToChanges to get the most recent data. but I'm not sure is this way to get data. The subscribeToChange method itself was supposed to send latest data. Question: I want to know what is best practice in order to get the data continuously in background from apple health only if there's an event update. I find out that many people has issue fetching background data and couldn't find any helpful resource. My requirement is to get updated data each time that is updated by apple watch or health app. Any hints on how to do it ?