agencyenterprise / react-native-health

A React Native package to interact with Apple HealthKit
MIT License
890 stars 238 forks source link

Doesn't work with new bridgeless architecture on 0.76.0 #395

Open andrewzey opened 3 weeks ago

andrewzey commented 3 weeks ago

Describe the bug Because the new react-native architecture lazy loads modules by default, require('react-native').NativeModules is an empty object at app startup time. Search for "Lazy module loading by default" in https://reactnative.dev/blog/2024/10/23/the-new-architecture-is-here

To Reproduce Load the app on React Native 0.76.0 and observe that all functions expected on AppleHealthKit are undefined.

Expected behavior All functions on AppleHealthKit are should be defined at access time.

Workaround Confirmed to Work Per this user's helpful comment (https://github.com/facebook/react-native/issues/29228#issuecomment-2345062090), this workaround fixes the issue for MY app which only uses a handful of functions from react-native-health. This is far from a global solution though. I'm using yarn so I'm able to simply do a yarn patch react-native-health to be unblocked with this change:

index.js

import { Activities, Observers, Permissions, Units } from './src/constants'

// const { AppleHealthKit } = require('react-native').NativeModules

// export const HealthKit = Object.assign({}, AppleHealthKit, {
//   Constants: {
//     Activities,
//     Observers,
//     Permissions,
//     Units,
//   },
// })

// Temporary workaround per:
// https://github.com/facebook/react-native/issues/29228#issuecomment-2345062090
const NativeModules = require('react-native').NativeModules
export const HealthKit = {
  isAvailable: NativeModules.AppleHealthKit.isAvailable,
  initHealthKit: NativeModules.AppleHealthKit.initHealthKit,
  getDailyStepCountSamples: NativeModules.AppleHealthKit.getDailyStepCountSamples,
  getSamples: NativeModules.AppleHealthKit.getSamples,

  Constants: {
    Activities,
    Observers,
    Permissions,
    Units,
  },
}

module.exports = HealthKit
andrewzey commented 3 weeks ago

An example of the error message a user would encounter is:

_reactNativeHealth.default.isAvailable is not a function (it is undefined)

Simulator Screenshot - iPhone 16 Pro - 2024-10-29 at 20 58 17

dhgavali commented 1 week ago

Native Emitter is not getting triggered in my case event after checking the apple health or updating the data.

My Project details : "react-native-health": "^1.19.0", "expo": "^51.0.38", "react-native": "0.74.5",

ureReason=No such process found}}}
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - HeartRateVariabilitySDNN
[fitcentive.debug.dylib] There is no listeners for healthKit:HeartRateVariabilitySDNN:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - HeartRateVariabilitySDNN
healthKit:HeartRateVariabilitySDNN:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - RestingHeartRate
[fitcentive.debug.dylib] There is no listeners for healthKit:RestingHeartRate:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - RestingHeartRate
healthKit:RestingHeartRate:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - BasalEnergyBurned
[fitcentive.debug.dylib] There is no listeners for healthKit:BasalEnergyBurned:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - BasalEnergyBurned
healthKit:BasalEnergyBurned:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - StepCount
[fitcentive.debug.dylib] There is no listeners for healthKit:StepCount:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - StepCount healthKit:StepCount:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - Workout
[fitcentive.debug.dylib] There is no listeners for healthKit:Workout:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - Workout healthKit:Workout:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - Walking
[fitcentive.debug.dylib] There is no listeners for healthKit:Walking:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - Walking healthKit:Walking:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - MindfulSession
[fitcentive.debug.dylib] There is no listeners for healthKit:MindfulSession:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - MindfulSession
healthKit:MindfulSession:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - ActiveEnergyBurned
[fitcentive.debug.dylib] There is no listeners for healthKit:ActiveEnergyBurned:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - ActiveEnergyBurned
healthKit:ActiveEnergyBurned:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - HeartRate
[fitcentive.debug.dylib] There is no listeners for healthKit:HeartRate:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - HeartRate healthKit:HeartRate:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - Vo2Max
[fitcentive.debug.dylib] There is no listeners for healthKit:Vo2Max:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - Vo2Max healthKit:Vo2Max:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - SleepAnalysis
[fitcentive.debug.dylib] There is no listeners for healthKit:SleepAnalysis:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - SleepAnalysis healthKit:SleepAnalysis:new
 LOG  *** samples 65
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - RestingHeartRate
[fitcentive.debug.dylib] There is no listeners for healthKit:RestingHeartRate:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - RestingHeartRate
healthKit:RestingHeartRate:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - HeartRateVariabilitySDNN
[fitcentive.debug.dylib] There is no listeners for healthKit:HeartRateVariabilitySDNN:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - HeartRateVariabilitySDNN
healthKit:HeartRateVariabilitySDNN:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - BasalEnergyBurned
[fitcentive.debug.dylib] There is no listeners for healthKit:BasalEnergyBurned:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - BasalEnergyBurned
healthKit:BasalEnergyBurned:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - MindfulSession
[fitcentive.debug.dylib] There is no listeners for healthKit:MindfulSession:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - MindfulSession
healthKit:MindfulSession:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - Workout
[fitcentive.debug.dylib] There is no listeners for healthKit:Workout:new
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - Workout healthKit:Workout:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - HeartRate
[fitcentive.debug.dylib] There is no listeners for healthKit:HeartRate:new``` These kind of logs for no listeners are printing on console. 

but sometimes it automatically works 
``` [fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - ActiveEnergyBurned
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - ActiveEnergyBurned
healthKit:ActiveEnergyBurned:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - Walking
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - Walking healthKit:Walking:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - Vo2Max
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - Vo2Max healthKit:Vo2Max:new
[fitcentive.debug.dylib] [HealthKit] New sample received from Apple HealthKit - SleepAnalysis
[fitcentive.debug.dylib] [HealthKit] New sample from Apple HealthKit processed - SleepAnalysis healthKit:SleepAnalysis:new

However in both of cases nativeEmitter is not getting triggered and I'm not able to get any data in background / foreground. There is no proper example in the documentation as well how does the background listening of data work. a very short explanation on that. Any hints on getting background data updates would be helpful. I want to get data every time when there is change in data for health app. however as of now it doesn't matter for me after how much interval I get the updated data. does anyone know the solution? In existing issues find that most of the users are facing background issue updated. sample code on background listeners would really help?