我们正在制作用于添加水样的水跟踪应用程序。数据直接从Apple Watch和iPhone应用程序添加到Health。其中一个功能是撤消。
我正在尝试从Apple Watch中取出水样,它工作正常。但是,如果我在iPhone上添加样本并将其从Apple Watch中删除,则不会删除样本。最糟糕的是,我没有收到任何错误,HealthKit回调会返回成功。
- (void)removeSampleWithCompletion:(void (^ _Nonnull)(bool success, NSError *_Nullable error))completion {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *myDate = [NSDate date];
NSDate *startDate = [calendar startOfDayForDate:myDate];
// the type you're trying to delete (this could be heart-beats/steps/water/calories/etc..)
HKQuantityType *waterType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryWater];
// the predicate used to execute the query
NSPredicate *queryPredicate = [HKSampleQuery predicateForSamplesWithStartDate:startDate
endDate:myDate
options:HKQueryOptionStrictEndDate];
// prepare the query
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:waterType
predicate:queryPredicate
limit:100
sortDescriptors:nil
resultsHandler:^(HKSampleQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable results, NSError * _Nullable error) {
if (!error) {
// Successfully retreived samples
// Filter only our samples. It is no possible to delete others.
NSMutableArray <HKSample *>*locassaSamples = [NSMutableArray new];
for (HKSample *sample in results) {
if ([sample.sourceRevision.source.bundleIdentifier isEqual:@"com.companyname.appname"]) {
[locassaSamples addObject:sample];
}
}
// Remove last
HKSample *theLast = locassaSamples.lastObject;
if (theLast == nil) {
return;
}
[self.healthStore deleteObject:theLast
withCompletion:^(BOOL success, NSError * _Nullable error) {
completion(success, error);
}];
return;
} else {
completion(FALSE, error);
}
}];
// Source http://stackoverflow.com/a/38263174/1162044
// last but not least, execute the query
[self.healthStore executeQuery:query];
}
更新。
我创建了一个雷达#28007271。
答案 0 :(得分:1)
这是一个已在iOS 10和watchOS 3中修复的已知问题。