Skip to content

Commit 0ec4463

Browse files
committed
issue-201 possible fix
1 parent d8a5eac commit 0ec4463

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

RCTAppleHealthKit/RCTAppleHealthKit+Queries.m

+16-11
Original file line numberDiff line numberDiff line change
@@ -865,11 +865,12 @@ - (void)setObserverForType:(HKSampleType *)sampleType
865865
return;
866866
}
867867

868-
[self sendEventWithName:successEvent body:@{}];
868+
NSLog(@"Emtting event: %@", successEvent);
869+
[self emitEventWithName:successEvent andPayload:@{}];
869870

870871
completionHandler();
871872

872-
NSLog(@"[HealthKit] New sample from Apple HealthKit processed - %@", type);
873+
NSLog(@"[HealthKit] New sample from Apple HealthKit processed (dep) - %@ %@", type, successEvent);
873874
}];
874875

875876

@@ -886,7 +887,7 @@ - (void)setObserverForType:(HKSampleType *)sampleType
886887

887888
[self.healthStore executeQuery:query];
888889

889-
[self sendEventWithName:successEvent body:@{}];
890+
[self emitEventWithName:successEvent andPayload:@{}];
890891
}];
891892
}
892893

@@ -919,16 +920,19 @@ - (void)setObserverForType:(HKSampleType *)sampleType
919920

920921
NSLog(@"[HealthKit] An error happened when receiving a new sample - %@", error.localizedDescription);
921922
if(self.hasListeners) {
922-
[self sendEventWithName:failureEvent body:@{}];
923+
[self emitEventWithName:failureEvent andPayload:@{}];
923924
}
924925
return;
925926
}
927+
926928
if(self.hasListeners) {
927-
[self sendEventWithName:successEvent body:@{}];
929+
[self emitEventWithName:successEvent andPayload:@{}];
930+
} else {
931+
NSLog(@"has no listeners for %@", successEvent);
928932
}
929933
completionHandler();
930934

931-
NSLog(@"[HealthKit] New sample from Apple HealthKit processed - %@", type);
935+
NSLog(@"[HealthKit] New sample from Apple HealthKit processed - %@ %@", type, successEvent);
932936
}];
933937

934938

@@ -941,15 +945,16 @@ - (void)setObserverForType:(HKSampleType *)sampleType
941945
if (error) {
942946
NSLog(@"[HealthKit] An error happened when setting up background observer - %@", error.localizedDescription);
943947
if(self.hasListeners) {
944-
[self sendEventWithName:failureEvent body:@{}];
948+
[self emitEventWithName:failureEvent andPayload:@{}];
945949
}
946950
return;
947951
}
948-
952+
NSLog(@"[HealthKit] Background delivery enabled for %@", type);
949953
[self.healthStore executeQuery:query];
950-
if(self.hasListeners) {
951-
[self sendEventWithName:successEvent body:@{}];
952-
}
954+
if(self.hasListeners) {
955+
NSLog(@"[HealthKit] Background observer set up for %@", type);
956+
[self emitEventWithName:successEvent andPayload:@{}];
957+
}
953958
}];
954959
}
955960

RCTAppleHealthKit/RCTAppleHealthKit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
- (HKHealthStore *)_initializeHealthStore;
2424
- (void)isHealthKitAvailable:(RCTResponseSenderBlock)callback;
2525
- (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
26-
- (void)checkPermission:(NSString *)input callback:(RCTResponseSenderBlock)callback;
2726
- (void)getModuleInfo:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
2827
- (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback;
2928
- (void)initializeBackgroundObservers:(RCTBridge *)bridge;
29+
- (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload;
3030

3131
@end

RCTAppleHealthKit/RCTAppleHealthKit.m

+47-18
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,35 @@
2727
#import <React/RCTBridgeModule.h>
2828
#import <React/RCTEventDispatcher.h>
2929

30-
RCTAppleHealthKit *shared;
3130

3231
@implementation RCTAppleHealthKit
3332

34-
@synthesize bridge = _bridge;
35-
3633
bool hasListeners;
3734

3835
RCT_EXPORT_MODULE();
3936

37+
38+
+ (id)allocWithZone:(NSZone *)zone {
39+
static RCTAppleHealthKit *sharedInstance = nil;
40+
static dispatch_once_t onceToken;
41+
dispatch_once(&onceToken, ^{
42+
sharedInstance = [super allocWithZone:zone];
43+
});
44+
return sharedInstance;
45+
}
46+
47+
+ (RCTCallableJSModules *)sharedJsModule {
48+
static RCTCallableJSModules *sharedJsModule = nil;
49+
static dispatch_once_t onceToken;
50+
dispatch_once(&onceToken, ^{
51+
sharedJsModule = [RCTCallableJSModules new];
52+
});
53+
return sharedJsModule;
54+
}
55+
4056
- (id) init
4157
{
42-
if (shared != nil) {
43-
return shared;
44-
}
45-
46-
self = [super init];
47-
shared = self;
48-
return self;
58+
return [super init];
4959
}
5060

5161
+ (BOOL)requiresMainQueueSetup
@@ -583,7 +593,7 @@ - (void)initializeHealthKit:(NSDictionary *)input callback:(RCTResponseSenderBlo
583593
[self.healthStore requestAuthorizationToShareTypes:writeDataTypes readTypes:readDataTypes completion:^(BOOL success, NSError *error) {
584594
if (!success) {
585595
NSString *errMsg = [NSString stringWithFormat:@"Error with HealthKit authorization: %@", error];
586-
NSLog(errMsg);
596+
NSLog(@"%@", errMsg);
587597
callback(@[RCTMakeError(errMsg, nil, nil)]);
588598
return;
589599
} else {
@@ -662,8 +672,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
662672
if(permissions != nil && [permissions objectForKey:@"read"] != nil && [permissions objectForKey:@"write"] != nil){
663673
NSArray* readPermsNamesArray = [permissions objectForKey:@"read"];
664674
NSArray* writePermsNamesArray = [permissions objectForKey:@"write"];
665-
readPermsArray = [self getReadPermsFromOptions:readPermsNamesArray];
666-
writePermsArray = [self getWritePermsFromOptions:writePermsNamesArray];
675+
readPermsArray = [[self getReadPermsFromOptions:readPermsNamesArray] allObjects];
676+
writePermsArray = [[self getWritePermsFromOptions:writePermsNamesArray] allObjects];
667677
} else {
668678
callback(@[RCTMakeError(@"permissions must be included in permissions object with read and write options", nil, nil)]);
669679
return;
@@ -696,10 +706,8 @@ - (void)getAuthorizationStatus:(NSDictionary *)input callback:(RCTResponseSender
696706
697707
This method must be called at the application:didFinishLaunchingWithOptions: method, in AppDelegate.m
698708
*/
699-
- (void)initializeBackgroundObservers:(RCTBridge *)bridge
709+
- (void)initializeBackgroundObservers:(RCTBridge *)bridge
700710
{
701-
NSLog(@"[HealthKit] Background observers will be added to the app");
702-
703711
[self _initializeHealthStore];
704712

705713
self.bridge = bridge;
@@ -751,14 +759,35 @@ - (void)initializeBackgroundObservers:(RCTBridge *)bridge
751759

752760
// Will be called when this module's first listener is added.
753761
-(void)startObserving {
762+
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
763+
for (NSString *notificationName in [self supportedEvents]) {
764+
[center addObserver:self
765+
selector:@selector(emitEventInternal:)
766+
name:notificationName
767+
object:nil];
768+
}
754769
self.hasListeners = YES;
755-
// Set up any upstream listeners or background tasks as necessary
770+
}
771+
772+
- (void)emitEventInternal:(NSNotification *)notification {
773+
if (self.hasListeners) {
774+
self.callableJSModules = [RCTAppleHealthKit sharedJsModule];
775+
[self.callableJSModules setBridge:self.bridge];
776+
[self sendEventWithName:notification.name
777+
body:notification.userInfo];
778+
}
779+
}
780+
781+
- (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload {
782+
[[NSNotificationCenter defaultCenter] postNotificationName:name
783+
object:self
784+
userInfo:payload];
756785
}
757786

758787
// Will be called when this module's last listener is removed, or on dealloc.
759788
-(void)stopObserving {
760789
self.hasListeners = NO;
761-
// Remove upstream listeners, stop unnecessary background tasks
790+
[[NSNotificationCenter defaultCenter] removeObserver:self];
762791
}
763792

764793
@end

0 commit comments

Comments
 (0)