13

I design app with CMPedometer and have one strange problem. I have logs from my client and i look this CMPedometerData, that i think really incorrect and i cant understand why this is so

[2017-04-11 20:16:34 +0000] CMPedometerData, startDate 2017-04-11 20:16:32 +0000 endDate 2017-04-11 20:18:41 +0000 steps 3 distance 2.130000000004657 floorsAscended (null) floorsDescended (null) currentPace (null) currentCadence (null) averageActivePace 0>

As you can see my client (i cant reproduce this error) got pedometerData from method startPedometerUpdatesFromDate and endDate 2017-04-11 20:18:41 is bigger than now 2017-04-11 20:16:34 (it was first CMPedometerData after startPedometerUpdatesFromDate was launched after returning from background - willEnterForeground method). Maybe someone has already encountered a similar problem?

The part of my code:

- (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;
          ...
    }];
}
Nikita
  • 154
  • 1
  • 13

1 Answers1

0

I don't think this can be avoided as it depends on too many external factors.

What you could do is use logic to filter/arrange the data as you know that any data received newer to "now" date will actually be before now, so if you are just interested in the steps or something similar then just get the count.

if you are actually interested in exact times and steps then i can think of 3 alternatives:

1) consider the time displaced data as corrupted.

2) Use a time from an outside source (a server) instead of the device time.

3) Do some complex time managment by using the internal clock as specified here: Is there a clock in iOS that can be used that cannot be changed by the user

Community
  • 1
  • 1
Pochi
  • 13,391
  • 3
  • 64
  • 104