0

I started working on an existing app and noticed that the time "fast-forwards" when the app has been in the background for a long time. I shut it down at (example) 09:30 minutes, when I open it at 14:21 it did all of the work but only then it starts updating the UI. In a few seconds, it races through all of the data from five minutes in background.

This is not acceptable behavior for my app, so this is what I have done:

  • Added background mode (location) to my capabilities
  • Of course I linked CoreMotion to use the gyro/accelerometer.

    self.motionManager = [[CMMotionManager alloc] init];
    
    //Gyroscope
    if([self.motionManager isGyroAvailable])
    {
        // Start the gyroscope if it is not active already
        if([self.motionManager isGyroActive] == NO)
        {
            // Update us 2 times a second
            // [self.motionManager setGyroUpdateInterval:1.0f / 1.0f];
    
            [_motionManager setAccelerometerUpdateInterval:0.1];
            [self.motionManager
             startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue]
             withHandler:^(CMAccelerometerData  *accelerometerData, NSError *error) {
                 if(error){
    
                     NSLog(@"%@", error);
                 }
                 else {
                     //NSString *x = [[NSString alloc] initWithFormat:@"%.02f",accelerometerData.acceleration.x];
    
                     float g = 9.80665f;
                     float AccelX = accelerometerData.acceleration.x * g;
                     float Accely = accelerometerData.acceleration.y * g;
                     float Accelz = accelerometerData.acceleration.z * g;
    
                     [self getSicknessIndicator:AccelX ay:Accely az:Accelz];
                 }
             }];
    
            //Receive the gyroscope data on this block
    
    
        }
    }
    

I'm not quite sure why this behavior happens and I also tried out a lot of other questions on StackOverflow but with no result, unfortunately. They mostly tell me to add the background mode.

jbehrens94
  • 2,356
  • 6
  • 31
  • 59

1 Answers1

0

After enabling the background mode (location), you need to setup a location-manager and then call startUpdatingLocation, as long as your location-manager is capable to receive location-updates, your app will receive all device-motion updates in real time (even when phone is locked).

Here is the link to Apple guide.

Here is the link to an answer having sample code for location manager.

Community
  • 1
  • 1
Zee
  • 1,865
  • 21
  • 42