Core Motion update accelerometer handler has strong reference to GameScene

I found out that startAccelerometerUpdates handler holds a strong reference to GameScene and prevents calling the deinit of GameScene

it could be rewritten something like that ( i hope it will save someone time):

func setupCoreMotion() {
motionManager.accelerometerUpdateInterval = 0.2
let queue = OperationQueue()
motionManager.startAccelerometerUpdates(to: queue,
withHandler:
{[weak self] accelerometerData, error in
guard let accelerometerData = accelerometerData else {
return
}
let acceleration = accelerometerData.acceleration
let currentXAcceleration = (self?.xAcceleration) ?? 0
self?.xAcceleration = CGFloat(acceleration.x) * 0.75 + currentXAcceleration * 0.25
})
}

Thanks for pointing this out! I’ll make a note for us to address this the next time we update the book.

This topic was automatically closed after 166 days. New replies are no longer allowed.