【发布时间】:2016-08-24 19:18:04
【问题描述】:
我们正在制作水跟踪应用程序,用户可以在其中添加水样。数据直接从 Apple Watch 和 iPhone 应用程序添加到健康。其中一项功能是撤消。
我正在尝试从 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。
【问题讨论】:
-
你在哪里注意到删除后的样本仍然存在?手机还是手表?
-
他们留在手机上的“健康”应用程序中。
标签: objective-c watchkit apple-watch watchos-2 healthkit