react-native-healthkit icon indicating copy to clipboard operation
react-native-healthkit copied to clipboard

HealthKit.subscribeToChanges does not work with RN New Architecture enabled

Open KesoGizmoYoshi opened this issue 1 year ago • 15 comments

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.

KesoGizmoYoshi avatar Sep 11 '24 12:09 KesoGizmoYoshi

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?

robertherber avatar Sep 18 '24 08:09 robertherber

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

KesoGizmoYoshi avatar Sep 24 '24 16:09 KesoGizmoYoshi

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

robertherber avatar Sep 24 '24 17:09 robertherber

I see! Anything else you want me to test?

KesoGizmoYoshi avatar Sep 25 '24 10:09 KesoGizmoYoshi

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 👍

robertherber avatar Sep 25 '24 13:09 robertherber

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

KesoGizmoYoshi avatar Nov 06 '24 12:11 KesoGizmoYoshi

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 ?

dhgavali avatar Nov 17 '24 17:11 dhgavali

Could anyone make it work?

myerekapan avatar Jan 26 '25 19:01 myerekapan

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

It looks to me dataUpdatedInDatabase is called in both cases - if that's the indication of subscribeToChanges callback being called? Or am I misinterpreting these logs?

robertherber avatar Jan 30 '25 09:01 robertherber

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

It looks to me dataUpdatedInDatabase is called in both cases - if that's the indication of subscribeToChanges callback being called? Or am I misinterpreting these logs?

Could you confirm it worked when the app was completely closed or just when it's backgrounded?

myerekapan avatar Feb 01 '25 19:02 myerekapan

I am new arch and also cannot get it to work at all

danielexpodaniel avatar Feb 21 '25 20:02 danielexpodaniel

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

It looks to me dataUpdatedInDatabase is called in both cases - if that's the indication of subscribeToChanges callback being called? Or am I misinterpreting these logs?

Yeah, i think you are correct about the callback being called. :)

KesoGizmoYoshi avatar Mar 12 '25 19:03 KesoGizmoYoshi

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

It looks to me dataUpdatedInDatabase is called in both cases - if that's the indication of subscribeToChanges callback being called? Or am I misinterpreting these logs?

Could you confirm it worked when the app was completely closed or just when it's backgrounded?

I dont really remember.

KesoGizmoYoshi avatar Mar 12 '25 19:03 KesoGizmoYoshi

Any updates on that?

fatihkayan20 avatar Apr 29 '25 07:04 fatihkayan20

@robertherber Any update on this? We're really struggling with this issue. Can you please tell us the next steps to resolve this issue?

Currently, there are no sync operations possible from our watch app to our react-native app

We're using expo: 52.0.38 sdk and @kingstinct/react-native-healthkit 8.2.0

zain148 avatar Apr 30 '25 06:04 zain148

@zain148 Hello, I struggling with this issue too. But, I wanna know, Did the background function work fine before?

Dzxwind avatar May 20 '25 10:05 Dzxwind