Skip to content

Commit c7fd726

Browse files
committed
📝 Update background observers docs
1 parent 2460e59 commit c7fd726

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

docs/background.md

+16-17
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ following:
2727
If you followed the [Background Processing](https://github.com/agencyenterprise/react-native-health#background-processing)
2828
step in the README, you can skip this one.
2929

30-
To setup that in your project, in XCode open your `ios/AppDelegate.m` file and add the
31-
following statements:
30+
To setup that in your project, in XCode open your `ios/AppDelegate.m` file and uncomment the
31+
lines with a comment `Uncomment this line to use background observers`:
32+
3233

3334
```objective-c
3435
#import "AppDelegate.h"
3536

3637
...
3738

38-
/* Add the library import at the top of AppDelegate.m */
39-
#import "RCTAppleHealthKit.h"
39+
// #import "RCTAppleHealthKit.h" // <-- Uncomment this line to use background observers
4040

4141
...
4242

@@ -50,8 +50,7 @@ following statements:
5050

5151
...
5252

53-
/* Add Background initializer for HealthKit */
54-
[[RCTAppleHealthKit new] initializeBackgroundObservers:bridge];
53+
// [[RCTAppleHealthKit new] initializeBackgroundObservers:bridge]; // <-- Uncomment this line to use background observers
5554

5655
...
5756

@@ -88,17 +87,17 @@ up observers for workouts, the events would have the following names:
8887
### Example
8988
9089
```typescript
91-
import { NativeAppEventEmitter } from 'react-native'
92-
93-
const callback = (): void => {
94-
/* Execute any data query */
95-
}
96-
97-
/* Register native listener that will be triggered when successfuly enabled */
98-
NativeAppEventEmitter.addListener('healthKit:HeartRate:setup:success', callback)
99-
100-
/* Register native listener that will be triggered on each update */
101-
NativeAppEventEmitter.addListener('healthKit:HeartRate:new', callback)
90+
import React, { useEffect } from 'react';
91+
import { NativeEventEmitter, NativeModules } from 'react-native';
92+
93+
useEffect(() => {
94+
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
95+
'healthKit:HeartRate:new',
96+
async () => {
97+
console.log('--> observer triggered');
98+
},
99+
);
100+
});
102101
```
103102

104103
When a new sample appears, in order to get the information you need to call

example/ios/example/AppDelegate.m

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4545
self.window.rootViewController = rootViewController;
4646
[self.window makeKeyAndVisible];
4747

48-
/* Add Background initializer for HealthKit */
4948
// [[RCTAppleHealthKit new] initializeBackgroundObservers:bridge]; // <-- Uncomment this line to use background observers
5049

5150
return YES;

0 commit comments

Comments
 (0)