0

I have an app with a view that, when loaded, gets regular device motion updates (20 per second) and uses them to display things on the screen. This was working fine using the old (deprecated) UIAccelerometer code.

I have since ported it to use Core Motion and the CMMotionManager but am having the problem that every time (except the first time) one of these views is loaded, only one or two updates are received in the first second or so, and then they start being received as expected (20 per second).

Relevant code in the view:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register to receive acceleration from motion manager.
    motionManager = [[CMMotionManager alloc] init];
    motionManager.deviceMotionUpdateInterval = 0.05;
    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
        withHandler:^(CMDeviceMotion *motion, NSError *error) {
            // Handle motion and update UI here.
        }
    ];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    // Stop the motion updates.
    [motionManager stopDeviceMotionUpdates];
}

- (IBAction)exitView:(id)sender {
    // Stop the motion updates.
    [motionManager stopDeviceMotionUpdates];
    // Pop the view from the navigation controller.
}

I've added some NSLog statements and can confirm that motion updates are being requested as soon as the view loads, and that the handler is only receiving updates as I described above.

Is there anything I'm missing that I can do to get the CoreMotion version to behave the same as the old version, i.e. without the delay?

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • Are you sure the thread isn't just busy doing something else for ~1 second, so the blocks are forced to wait? Try creating a new `NSOperationQueue` instead of using `[NSOperationQueue currentQueue]`. Obviously make sure you `dispatch_async()` back to the main thread for any UI updates from the other queue. – Abhi Beckert Jun 10 '14 at 22:36
  • I've tried that and the motion updates are received straightaway but the stuff on the UI thread experiences the same delay. – DanielGibbs Jun 11 '14 at 18:11
  • OK so that means your main thread is busy doing something for an entire second. You should look into it with Instruments and fix it. It's a bad user experience for the main thread to spend a whole second doing anything. Generally anything taking more than 1/10th of a second should not happen on the main thread. – Abhi Beckert Jun 11 '14 at 21:09
  • Will do, although there's really not much going on at all. Also, it works perfectly the very first time the user loads the view. – DanielGibbs Jun 11 '14 at 21:11
  • It might be autorelease locking the thread, in which case you need some @autoreleasepool blocks somewhere. Or perhaps something is loading a URL on the main thread. – Abhi Beckert Jun 11 '14 at 21:18
  • I've had a look in instruments and can't see anything unusual, there are no URLs in there at all, and @autoreleasepool doesn't seem to help. – DanielGibbs Jun 15 '14 at 20:10

0 Answers0