Search code examples
iosswiftsprite-kitinstrumentsappdelegate

SpriteKit Game Spending Lots of Time in AppDelegate


I'm profiling my app with Xcode's Instruments and as I play a level, I notice the frame rate drop every now and then. If I select a 500ms period where the frame rate dropped, I see that 275ms of that time is spent in the AppDelegate. I'm not doing anything special in there - basically just the boilerplate code. Has anyone else run into this issue?

Running Time    Self (ms)   Symbol Name

275.0ms 100.0%  258.0       main

Here's my AppDelegate code:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        print("about to enter background")
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        print("entered background")
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        print("will become active")
        //NSNotificationCenter.defaultCenter().postNotificationName("PauseGame", object: self)
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

Solution

  • It's normal. The app delegate is effectively the root object of your app. Like the UIApplication object itself, the app delegate is a singleton object and is always present at runtime.

    The app delegate performs several crucial roles:

    • It contains your app’s startup code.
    • It responds to key changes in the state of your app. Specifically, it responds to both temporary interruptions and to changes in the execution state of your app, such as when your app transitions from the foreground to the background.
    • It responds to notifications originating from outside the app, such as remote notifications (also known as push notifications), low-memory warnings, download completion notifications, and more.
    • It determines whether state preservation and restoration should occur and assists in the preservation and restoration process as needed.
    • It responds to events that target the app itself and are not specific to your app’s views or view controllers.

    One of the main jobs of the app delegate is to respond to state transitions reported by the system. For every state change that occurs, the system calls the appropriate methods of the app delegate. Each state has different rules that govern how the app should is expected to behave and the app delegate methods must adjust the behavior of the app accordingly.

    For more information please see this official Apple guide