SpriteKit 游戏在 AppDelegate 中花费大量时间
Posted
技术标签:
【中文标题】SpriteKit 游戏在 AppDelegate 中花费大量时间【英文标题】:SpriteKit Game Spending Lots of Time in AppDelegate 【发布时间】:2016-06-16 12:33:50 【问题描述】:我正在使用 Xcode 的 Instruments 分析我的应用程序,当我玩一个关卡时,我注意到帧率时不时地下降。如果我选择帧速率下降的 500 毫秒时间段,我会看到其中 275 毫秒的时间用于 AppDelegate。我在那里没有做任何特别的事情——基本上只是样板代码。有没有其他人遇到过这个问题?
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】:这很正常。应用程序委托实际上是应用程序的根对象。与UIApplication
对象本身一样,应用程序委托是一个单例对象,并且始终存在于运行时。
应用代理执行几个关键角色:
它包含您应用的启动代码。 它响应应用程序状态的关键变化。具体来说,它 对临时中断和在 您的应用的执行状态,例如当您的应用从 从前景到背景。 它响应来自应用程序外部的通知,例如 作为远程通知(也称为推送通知), 内存不足警告、下载完成通知等。 它决定是否应该进行状态保存和恢复 并根据需要协助保存和恢复过程。 它响应以应用程序本身为目标且不特定的事件 到您应用的视图或视图控制器。应用委托的主要工作之一是响应系统报告的状态转换。对于发生的每个状态更改,系统都会调用应用程序委托的适当方法。每个状态都有不同的规则来管理应用程序的预期行为方式,并且应用程序委托方法必须相应地调整应用程序的行为。
更多信息请看苹果官方guide
【讨论】:
感谢您的详细解答!以上是关于SpriteKit 游戏在 AppDelegate 中花费大量时间的主要内容,如果未能解决你的问题,请参考以下文章