diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index 45f8720a1356cb15020b7d897c668329f1b5ea17..b1d0e4aefe9c62b687e330a9631812aa0b43b60d 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -208,23 +208,20 @@ - (void)setObserverForType:(HKSampleType *)type unit:(HKUnit *)unit { - NSLog(@"set observer"); HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:type predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error){ - UIApplication *app = [UIApplication sharedApplication]; - NSLog(@"observer fired"); - [self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""]; - completionHandler(); -// self.isSync = true; -// __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{ -// -// NSLog(@"observer fired from bg"); -// [self.bridge.eventDispatcher sendAppEventWithName:@"observer" -// body:@""]; -// -// [app endBackgroundTask:backgroundTaskIdentifier]; + if (error) { + NSLog(@"*** An error occured while setting up the stepCount observer. %@ ***", error.localizedDescription); + return; + } + [self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""]; + + // Theoretically, HealthKit expect that copletionHandler would be called at the end of query process, + // but it's unclear how to do in in event paradigm + +// dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 5); +// dispatch_after(delay, dispatch_get_main_queue(), ^(void){ // completionHandler(); -// self.isSync = false; -// }]; +// }); }]; [self.healthStore executeQuery:query]; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index d26a7080835a02883d89a88eb8c05a0446d1bc59..41cd34e110052b41cd706ad9ae241244162b9540 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -22,6 +22,7 @@ #import @implementation RCTAppleHealthKit + @synthesize bridge = _bridge; RCT_EXPORT_MODULE();