我们有一个在生产中的应用程序,报告说非常高的时间交互(Tti)的ios 15预热。
TTI = timewhenViewController被加载- mainStartTime
mainStart时间在AppDelegate.Swive的willFinishLaunchingWithOptions方法中进行测量,如下所示
mainStartTime = Date()
当加载第一个视图控制器时,我们将tti测量为
tti = -(mainStartTime.timeIntervalSinceNow)
我们观察到,对于预暖的场景,mainStartTime来得非常早(用户甚至在启动该应用程序前2小时左右)。
我上网查了一下,但没有找到任何文件。我只想知道,在预置应用程序的同时调用willFinishLaunchingWithOptions方法会不会发生这种情况。
发布于 2022-02-10 23:12:11
关于预热问题,有很差的文档:
当
()调用UIApplicationMain(:)时,main()会执行应用程序的启动序列,但不包括。https://developer.apple.com/documentation/uikit/app_and_environment/responding_to_the_launch_of_your_app/about_the_app_launch_sequence#3894431
但是预热可以调用willFinishLaunchingWithOptions
和didFinishLaunchingWithOptions
,这是我在iPhone 13上使用iOS 15.0进行的测试。
我创建了一个测试应用程序,它记录对字符串数组的所有调用:
main.swift
var logMessages = [String]()
func addLogMessage(_ text: String) {
let date = Date().formatted(date: .omitted, time: .standard)
logMessages.append("\(date): \(text)")
}
autoreleasepool {
addLogMessage("main.swift")
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))
}
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
override init() {
super.init()
addLogMessage("AppDelegate.init")
}
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
addLogMessage("willFinishLaunchingWithOptions")
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
addLogMessage("didFinishLaunchingWithOptions")
return true
}
func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) {
addLogMessage("applicationProtectedDataDidBecomeAvailable")
}
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
addLogMessage("configurationForConnecting")
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
…
}
SceneDelegate.swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow? {
didSet {
addLogMessage("SceneDelegate.didSet window")
}
}
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
addLogMessage("SceneDelegate.willConnectTo")
guard let _ = (scene as? UIWindowScene) else { return }
}
…
}
然后做了以下工作:
briefly
我的日志:
12:28:36 AM: main.swift
12:28:36 AM: AppDelegate.init
12:28:36 AM: willFinishLaunchingWithOptions
12:28:36 AM: didFinishLaunchingWithOptions
12:58:15 AM: applicationProtectedDataDidBecomeAvailable
12:58:15 AM: configurationForConnecting
12:58:15 AM: SceneDelegate.didSet window
12:58:15 AM: SceneDelegate.willConnectTo
12:58:15 AM: viewDidLoad
如您所见,系统在12:28:36启动了我的应用程序(预置),并在后台保存到12:58:15手动启动,并访问了受保护的数据和加载的UI之后。
发布于 2022-02-23 09:59:39
苹果的文档是不正确的,以下是在iOS 15上观察到的行为:
UIApplicationMain()
总是运行,包括在预热期间。
之后会发生什么取决于应用程序是否使用UIScene
生命周期。
用于支持场景的应用程序的
- `application:didFinishLaunchingWithOptions:` may be called (doesn't always happen)
- `scene:willConnectToSession:options:` is not called - in fact the `SceneDelegate` is not created until the app is opened.
不支持场景的应用程序的
- `application:didFinishLaunchingWithOptions:` is not called.
https://stackoverflow.com/questions/71025205
复制相似问题