Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DanStevensonDev opened this issue Jun 10, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@DanStevensonDev
Copy link

DanStevensonDev commented Jun 10, 2024

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):

  • Device: iPhone 12 mini
  • OS: iOS 15.5.1
  • Version 21F90

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];
}
@DanStevensonDev DanStevensonDev added the bug Something isn't working label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant