agencyenterprise / react-native-health

A React Native package to interact with Apple HealthKit
MIT License
799 stars 226 forks source link

NativeEventEmitter not firing after steps data has logged (only fires after checking Apple Health first) #378

Open DanStevensonDev opened 3 weeks ago

DanStevensonDev commented 3 weeks ago

Describe the bug My NativeEventEmitter is not firing when I know there should be new background data for steps. It fires only when I check the updated data in Apple Health first.

To Reproduce Steps to reproduce the behavior:

Scenario 1

  1. walk around with iPhone, thereby logging more step data
  2. open my app
  3. NativeEventEmitter does not fire

Scenario 2

  1. walk around with iPhone, thereby logging more step data
  2. open the Apple Health app, seeing the step count has increased
  3. open my app
  4. NativeEventEmitter does fire and new data is set/visible

Expected behavior

The NativeEventEmitter should fire when opening my app, without me having to see the new data in the Apple Health app first.

Users of my app expect their steps data to be accurate inside my app, without having to use Apple Health.

Smartphone (please complete the following information):

Additional context

Implementation of event listener:

    const [hkStepCount, setHkStepCount] = useState(0);
    // listeners for AppleHealth data changes
    useEffect(() => {
        const options = {
            startDate: dayjs().startOf("D").toISOString()
        };

        new NativeEventEmitter(NativeModules.AppleHealthKit).addListener("healthKit:StepCount:new", async () => {
            AppleHealthKit.getStepCount(options, (err: string, results: HealthValue) => {
                                 // this doesn't log when it should
                console.log("*** getStepCount: ");
                if (err) console.log("err: ", JSON.stringify(err, null, 1));
                                 // `title` here is related to my component logic
                if (results && title === "Steps") {
                    console.log("listener results: ", JSON.stringify(results, null, 1));
                    setHkStepCount(results.value);
                }
            });
        });
    });

Permissions that have been granted:

const { Permissions } = AppleHealthKit.Constants;

            const permissions = {
                permissions: {
                    read: [Permissions.Steps]
                }
            } as HealthKitPermissions;

            AppleHealthKit.initHealthKit(permissions, (error: string) => {
...
            });

From AppDelegate.mm:

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  self.moduleName = @"main";
  self.initialProps = @{};

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self
                                            launchOptions:launchOptions];

  /* Add Background initializer for HealthKit  */
  [[RCTAppleHealthKit new] initializeBackgroundObservers:bridge];

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}