diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h index bf37c55c6b13f8ae4c3cd1df2cabc95bdd048408..32fb563c2ceabfc5cddb294b56aa0bc92060c903 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.h @@ -12,7 +12,7 @@ - (void)fitness_getStepCountOnDay:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_getSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; -- (void)fitness_setObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; +- (void)fitness_setObserver:(NSDictionary *)input; - (void)fitness_getDailyStepSamples:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_saveSteps:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; - (void)fitness_initializeStepEventObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback; diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m index 5e6fc0d7c4ecff9784bcfe180a28921fffc7b543..294c9f1603ae4ada4ce42838816c08d4fa89fa44 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Methods_Fitness.m @@ -91,7 +91,7 @@ }]; } -- (void)fitness_setObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback +- (void)fitness_setObserver:(NSDictionary *)input { HKUnit *unit = [RCTAppleHealthKit hkUnitFromOptions:input key:@"unit" withDefault:[HKUnit countUnit]]; NSString *type = [RCTAppleHealthKit stringFromOptions:input key:@"type" withDefault:@"Walking"]; @@ -111,9 +111,7 @@ samplesType = [HKObjectType workoutType]; } - [self setObserverForType:samplesType unit:unit completion:^(NSArray *results, NSError * error) { - callback(@[[NSNull null], results]); - }]; + [self setObserverForType:samplesType unit:unit]; } diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h index 8eec34b4a6e4739025ea621ed1759ed554315f5a..381a82c5b778988beb1376810c2b383f4198203d 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.h @@ -27,8 +27,7 @@ limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion; - (void)setObserverForType:(HKSampleType *)quantityType - unit:(HKUnit *)unit - completion:(void (^)(NSArray *, NSError *))completion; + unit:(HKUnit *)unit; - (void)fetchQuantitySamplesOfType:(HKQuantityType *)quantityType diff --git a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m index d84600ae890125f345b21d17dbff477e1e7d345b..7e73b656eb1015aa02c2a12a583e0a7ee3811b9c 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit+Queries.m @@ -14,7 +14,6 @@ @implementation RCTAppleHealthKit (Queries) - - (void)fetchMostRecentQuantitySampleOfType:(HKQuantityType *)quantityType predicate:(NSPredicate *)predicate completion:(void (^)(HKQuantity *, NSDate *, NSDate *, NSError *))completion { @@ -203,58 +202,25 @@ } - (void)setObserverForType:(HKSampleType *)type - unit:(HKUnit *)unit - completion:(void (^)(NSArray *, NSError *))completion { + unit:(HKUnit *)unit { NSLog(@"set observer"); - void(^handler)(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error); - handler = ^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error) { - NSLog(@"enter to a handler %@", [error localizedDescription]); + HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:type predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError * _Nullable error){ UIApplication *app = [UIApplication sharedApplication]; - UIApplicationState appState = [app applicationState]; - - if (!self.isSync){ -// switch (appState) { -// case UIApplicationStateActive: -// NSLog(@"app Active try to make sync"); -// break; -// case UIApplicationStateInactive: -// { -// NSLog(@"enter to inactive case"); -// break; -// } -// case UIApplicationStateBackground: -// { -// NSLog(@"enter to background case"); -// self.isSync = true; -// __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{ + NSLog(@"observer fired"); + [self.bridge.eventDispatcher sendAppEventWithName:@"observer" body:@""]; + completionHandler(); +// self.isSync = true; +// __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{ // -// NSLog(@"call event background"); -//// [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" -//// body:@{@"name": @"change:steps"}]; -// [self fetchSamplesOfType:type unit:unit predicate:nil ascending:true limit:1 completion:completion]; -// [app endBackgroundTask:backgroundTaskIdentifier]; -// self.isSync = false; -// }]; -// } +// NSLog(@"observer fired from bg"); +// [self.bridge.eventDispatcher sendAppEventWithName:@"observer" +// body:@""]; // -// }; - - NSLog(@"enter to background case"); - self.isSync = true; - __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{ - - NSLog(@"call event background"); - [self.bridge.eventDispatcher sendAppEventWithName:@"change:steps" - body:@{@"name": @"change:steps"}]; - [self fetchSamplesOfType:type unit:unit predicate:nil ascending:true limit:1 completion:completion]; - [app endBackgroundTask:backgroundTaskIdentifier]; - completionHandler(); - self.isSync = false; - }]; - } - - }; - HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:type predicate:nil updateHandler:handler]; +// [app endBackgroundTask:backgroundTaskIdentifier]; +// completionHandler(); +// self.isSync = false; +// }]; + }]; [self.healthStore executeQuery:query]; [self.healthStore enableBackgroundDeliveryForType:type frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError * _Nullable error) { @@ -262,9 +228,6 @@ }]; } - - - - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate limit:(NSUInteger)lim completion:(void (^)(NSArray *, NSError *))completion { diff --git a/RCTAppleHealthKit/RCTAppleHealthKit.m b/RCTAppleHealthKit/RCTAppleHealthKit.m index 2d1fa49eb646e715a9a8053188273760da144c1e..bbc9c171fecf79d630933c2c64345d1bd81e5d43 100644 --- a/RCTAppleHealthKit/RCTAppleHealthKit.m +++ b/RCTAppleHealthKit/RCTAppleHealthKit.m @@ -111,9 +111,9 @@ RCT_EXPORT_METHOD(getSamples:(NSDictionary *)input callback:(RCTResponseSenderBl [self fitness_getSamples:input callback:callback]; } -RCT_EXPORT_METHOD(setObserver:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) +RCT_EXPORT_METHOD(setObserver:(NSDictionary *)input) { - [self fitness_setObserver:input callback:callback]; + [self fitness_setObserver:input]; }