@@ -348,7 +348,7 @@ - (void)fetchSamplesOfType:(HKSampleType *)type
348
348
@" activityName" : type,
349
349
@" calories" : @(energy),
350
350
@" tracked" : @(isTracked),
351
- @" metadata" : [sample metadata ],
351
+ @" metadata" : [sample metadata ] ? [sample metadata ] : [ NSNull null ] ,
352
352
@" sourceName" : [[[sample sourceRevision ] source ] name ],
353
353
@" sourceId" : [[[sample sourceRevision ] source ] bundleIdentifier ],
354
354
@" device" : device,
@@ -614,6 +614,21 @@ - (void)fetchSleepCategorySamplesForPredicate:(NSPredicate *)predicate
614
614
case HKCategoryValueSleepAnalysisAsleep:
615
615
valueString = @" ASLEEP" ;
616
616
break ;
617
+
618
+ // watchOS 9 and iOS 16 introduce Core, Deep, REM, and Awake phases of sleep.
619
+ case HKCategoryValueSleepAnalysisAsleepCore:
620
+ valueString = @" CORE" ;
621
+ break ;
622
+ case HKCategoryValueSleepAnalysisAsleepDeep:
623
+ valueString = @" DEEP" ;
624
+ break ;
625
+ case HKCategoryValueSleepAnalysisAsleepREM:
626
+ valueString = @" REM" ;
627
+ break ;
628
+ case HKCategoryValueSleepAnalysisAwake:
629
+ valueString = @" AWAKE" ;
630
+ break ;
631
+
617
632
default :
618
633
valueString = @" UNKNOWN" ;
619
634
break ;
@@ -741,6 +756,12 @@ - (void)fetchSumOfSamplesOnDayForType:(HKQuantityType *)quantityType
741
756
NSDate *startDate = result.startDate ;
742
757
NSDate *endDate = result.endDate ;
743
758
double value = [sum doubleValueForUnit: unit];
759
+ if (startDate == nil || endDate == nil ) {
760
+ error = [[NSError alloc ] initWithDomain: @" AppleHealthKit"
761
+ code: 0
762
+ userInfo: @{@" Error reason" : @" Could not fetch statistics: Not authorized" }
763
+ ];
764
+ }
744
765
completionHandler (value, startDate, endDate, error);
745
766
}
746
767
}];
@@ -1041,11 +1062,12 @@ - (void)setObserverForType:(HKSampleType *)sampleType
1041
1062
return ;
1042
1063
}
1043
1064
1044
- [self sendEventWithName: successEvent body: @{}];
1065
+ NSLog (@" Emitting event: %@ " , successEvent);
1066
+ [self emitEventWithName: successEvent andPayload: @{}];
1045
1067
1046
1068
completionHandler ();
1047
1069
1048
- NSLog (@" [HealthKit] New sample from Apple HealthKit processed - %@ " , type);
1070
+ NSLog (@" [HealthKit] New sample from Apple HealthKit processed (dep) - %@ %@ " , type, successEvent );
1049
1071
}];
1050
1072
1051
1073
@@ -1062,7 +1084,7 @@ - (void)setObserverForType:(HKSampleType *)sampleType
1062
1084
1063
1085
[self .healthStore executeQuery: query];
1064
1086
1065
- [self sendEventWithName : successEvent body : @{}];
1087
+ [self emitEventWithName : successEvent andPayload : @{}];
1066
1088
}];
1067
1089
}
1068
1090
@@ -1095,16 +1117,19 @@ - (void)setObserverForType:(HKSampleType *)sampleType
1095
1117
1096
1118
NSLog (@" [HealthKit] An error happened when receiving a new sample - %@ " , error.localizedDescription );
1097
1119
if (self.hasListeners ) {
1098
- [self sendEventWithName : failureEvent body : @{}];
1120
+ [self emitEventWithName : failureEvent andPayload : @{}];
1099
1121
}
1100
1122
return ;
1101
1123
}
1124
+
1102
1125
if (self.hasListeners ) {
1103
- [self sendEventWithName: successEvent body: @{}];
1126
+ [self emitEventWithName: successEvent andPayload: @{}];
1127
+ } else {
1128
+ NSLog (@" There is no listeners for %@ " , successEvent);
1104
1129
}
1105
1130
completionHandler ();
1106
1131
1107
- NSLog (@" [HealthKit] New sample from Apple HealthKit processed - %@ " , type);
1132
+ NSLog (@" [HealthKit] New sample from Apple HealthKit processed - %@ %@ " , type, successEvent );
1108
1133
}];
1109
1134
1110
1135
@@ -1117,15 +1142,16 @@ - (void)setObserverForType:(HKSampleType *)sampleType
1117
1142
if (error) {
1118
1143
NSLog (@" [HealthKit] An error happened when setting up background observer - %@ " , error.localizedDescription );
1119
1144
if (self.hasListeners ) {
1120
- [self sendEventWithName : failureEvent body : @{}];
1145
+ [self emitEventWithName : failureEvent andPayload : @{}];
1121
1146
}
1122
1147
return ;
1123
1148
}
1124
-
1149
+ NSLog ( @" [HealthKit] Background delivery enabled for %@ " , type);
1125
1150
[self .healthStore executeQuery: query];
1126
- if (self.hasListeners ) {
1127
- [self sendEventWithName: successEvent body: @{}];
1128
- }
1151
+ if (self.hasListeners ) {
1152
+ NSLog (@" [HealthKit] Background observer set up for %@ " , type);
1153
+ [self emitEventWithName: successEvent andPayload: @{}];
1154
+ }
1129
1155
}];
1130
1156
}
1131
1157
0 commit comments