Retain cycle suspected in closure

2019-07-30 20:16发布

问题:

I suspect that the following function, which I use in my GameScene class in order to manage the accelerometer's input, is keeping my scene from deinitializing when I transition to another scene:

class GameScene: SKScene {
    let motionManager = CMMotionManager()
    var xAcceleration = CGFloat(0)
    // Some stuff
    // override func didMove(to: ....

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

It may be because of the self capture, but if that's the case, I have no clue where to put the "[unowned self] in" capture list.

回答1:

You should put it before accelerometerData, error in like this [unowned self] accelerometerData, error in