Search code examples
iosswiftxcodescenekit

iOS SceneKit application faster in App Switcher Mode


I'm encountering a rather bizarre behavior with a SceneKit application that I am developing. I invoke a method which manipulates the simdWorldPosition property of various SCNNode objects. The method changes the simdWorldPosition in a loop which can execute around ~40k times.

I define a DispatchQueue:

let updateQueue = DispatchQueue(label: Bundle.main.bundleIdentifier! +
        ".serialSceneKitQueue")

A button press calls the following:

@objc func didPressGreedy() {
    updateQueue.async {
        let _ = self.Greedy()
    }
}
autoreleasepool {
    for offset in stride(from: 0.0, through: planeWidth, by: increment) {
        var topLeftTransform = arkitAnchor!.simdWorldTransform * HelperMethods.makeTranslationMatrix(tx: -Float(planeWidth / 2), ty: 0.0, tz: -Float(planeHeight/2))
        topLeftTransform = topLeftTransform * HelperMethods.makeTranslationMatrix(tx: Float(offset), ty: 0.0, tz: 0.0)
        let topEdgePosition = simd_float3(x: topLeftTransform.columns.3.x, y: topLeftTransform.columns.3.y, z: topLeftTransform.columns.3.z)
        saLabelComponent.simdWorldPosition = topEdgePosition

        let oldSpriteTransform = sprite.simdWorldTransform

        for xOffset in stride(from:-0.20, through: 0.20, by: increment) {
            for yOffset in stride(from: 0.05, through: 0.20, by: increment) {
                LOOP_1_ITERATIONS += 1
                var newSpriteTransform = anchor.simdWorldTransform * HelperMethods.makeTranslationMatrix(tx: Float(xOffset), ty: Float(yOffset), tz: 0.0)
                sprite.simdWorldTransform = newSpriteTransform
            }
        }
    }
}

These iterations are slow - I can sometimes see about a thousand manipulations happen in multiple seconds and then it takes a minute to do 50 simdWorldPosition updates. If I tap on the screen I can see the executions speed up.

When I use the iOS App Switcher (by just swiping up on the application on iPhone XS and letting it sit in the App Switcher) the application significantly speeds up the execution the iterations by a factor of 10x.

Any idea of why this is caused? If I am having conceptual issues please point to relevant topic/articles.


Solution

  • Any idea of why this is caused?

    It sounds like your app is simply doing a lot less work when in switcher. That's not surprising... the area that the app draws into is much smaller, there are no touch events to handle, etc. The difference may have less to do with the code in the loop you've shown than in the fact that the rest of the app isn't doing as much.

    If you really want to know what's going on (or what isn't going on), the best thing to do is to profile your code with Instruments.