我用 CMPedometer 设计应用程序并遇到一个奇怪的问题。我有来自我的客户的日志,我查看了这个 CMPedometerData,我认为这真的不正确,我不明白为什么会这样
[2017-04-11 20:16:34 +0000] CMPedometerData, startDate 2017-04-11 20:16:32 +0000 endDate 2017-04-11 20:18:41 +0000 步 3 距离 2.130000000004657 楼层上升(空) floorDescended (null) currentPace (null) currentCadence (null) averageActivePace 0>
如您所见,我的客户(我无法重现此错误)从方法startPedometerUpdatesFromDate
和 endDate 获得 pedometerData 2017-04-11 20:18:41 大于现在 2017-04-11 20:16:34 (它是 startPedometerUpdatesFromDate 之后的第一个 CMPedometerData从后台返回后启动 -willEnterForeground
方法)。也许有人已经遇到过类似的问题?
我的代码部分:
- (void)didEnterBackground {
dispatch_async(dispatch_get_main_queue(), ^{
[[Pedometer sharedInstance].motionActivityManager stopActivityUpdates];
[[Pedometer sharedInstance].pedometer stopPedometerUpdates];
});
}
- (void)willEnterForeground {
NSDate *nowDate = [NSDate new];
/* here is request to get historical data from lastDateUpdate (store in database) to now date */
[[Pedometer sharedInstance] importDataFrom:lastDateUpdate endDate:nowDate completion:^{
dispatch_async(dispatch_get_main_queue(), ^{
/* show info */
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self startUpdatingData:nowDate];
});
lastDateUpdate = nowDate;
}
- (void)startUpdatingData:(NSDate *)fromDate {
NSOperationQueue *activityQueue = [[NSOperationQueue alloc] init];
[[Pedometer sharedInstance].motionActivityManager startActivityUpdatesToQueue:activityQueue withHandler:^(CMMotionActivity * _Nullable act) {
...
}];
[[Pedometer sharedInstance].pedometer startPedometerUpdatesFromDate:fromDate withHandler:^(CMPedometerData * _Nullable pedometerData1, NSError * _Nullable error) {
...
NSLog(@"%@", pedometerData1);
...
lastDateUpdate = pedometerData1.endDate;
...
}];
}