SpriteKit游戏在AppDelegate中花费了大量时间

时间:2016-06-16 12:33:51

标签: ios swift sprite-kit instruments appdelegate

我使用Xcode的乐器分析我的应用程序,当我播放关卡时,我时不时地注意到帧速率下降。如果我选择帧速率下降的500ms周期,我会看到在AppDelegate中花费了275ms的时间。我没有在那里做任何特别的事 - 基本上只是样板代码。还有其他人遇到过这个问题吗?

Running Time    Self (ms)   Symbol Name

275.0ms 100.0%  258.0       main

这是我的AppDelegate代码:

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:.
    }


}

1 个答案:

答案 0 :(得分:0)

这很正常。应用程序委托实际上是应用程序的根对象。与UIApplication对象本身一样,app委托是一个单例对象,并且始终存在于运行时。

app委托执行几个关键角色:

  • 它包含您应用的启动代码。
  • 它会响应您应用状态的主要变化。具体来说,它 响应临时中断和变化 应用的执行状态,例如应用转换时的状态 背景的前景。
  • 它响应来自应用程序外部的通知,例如 作为远程通知(也称为推送通知), 低内存警告,下载完成通知等。
  • 确定是否应该进行状态保存和恢复 并根据需要协助保存和恢复过程。
  • 它响应针对应用程序本身的事件,而不是特定的事件 到你的应用程序的视图或查看控制器。

app委托的主要工作之一是响应系统报告的状态转换。对于发生的每个状态更改,系统都会调用app委托的相应方法。每个州都有不同的规则来管理应用程序应该如何运行,并且应用程序委托方法必须相应地调整应用程序的行为。

有关详细信息,请参阅此官方Apple [{3}}