Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Main Thread Checker warning with CoreMotion, only appearing on 2018 model iPhones

The following is an extremely simplified version of the issue:

import UIKit
import CoreMotion

class ViewController: UIViewController {
    private let manager = CMMotionManager()
    private let interval = 0.01

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        startUpdates()
    }

    private func startUpdates() {
        let queue = OperationQueue()
        queue.name = "com.demo.motion"
        queue.qualityOfService = .userInteractive
        queue.maxConcurrentOperationCount = 1
        manager.accelerometerUpdateInterval = interval
        manager.startAccelerometerUpdates(to: queue) { _, _ in }
    }
}

When I run this on an iPhone XR, I will receive the following:

Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState] PID: 1237, TID: 147763, Thread name: com.apple.CoreMotion.MotionThread, Queue name: com.apple.root.default-qos.overcommit, QoS: 0

  • My device is running 12.3, which is currently the latest iOS release
  • Running this on various iPads (pre 2018), iPhone 8, iPhone 6S, and other older models, the issue does not appear.
  • Changing the qualityOfService has no impact on this (my preference is to use utility)
  • Out of curiosity, I wrapped the entire startUpdates() function around a main dispatch with no success

Based on the queue name and when this is occurring, my assumption is that this is something internal that is just getting exposed by the faster processor in the XR, versus the older devices.

like image 793
CodeBender Avatar asked Feb 09 '19 15:02

CodeBender


1 Answers

New answer when using iOS 13

I have tested this just now with the newly released iOS 13 version (17A577) on a 2018 device and it no longer produces the threading error.

Previous answer when using iOS 12

This is a known issue that has been reported to Apple with a radar linked here.
like image 168
CodeBender Avatar answered Sep 21 '22 18:09

CodeBender