【问题标题】:Reading Bundle.main.path at the launching of the app is time consuming or not?在应用程序启动时读取 Bundle.main.path 是否耗时?
【发布时间】:2021-05-11 17:13:02
【问题描述】:
对于特定于应用程序,我有与环境和配置相关的 json 文件,在应用程序启动时读取这些文件是否耗时? (当然我在启动时需要它)如果是的话,我将使用脚本创建结构常量
Bundle.main.path(forResource: ConfigPath.urlString(api: .environment), ofType: "json")
【问题讨论】:
标签:
ios
objective-c
swift
xcode
swift3
【解决方案1】:
读取或写入文件是最耗时的操作之一。无论您是否在应用程序启动时执行此操作。如果您想检查它如何影响启动时间,您可以创建一个简单的 UI 测试并在有和没有文件操作的情况下运行它。
import XCTest
class UILaunchTests: XCTestCase {
func testLaunchPerformance() {
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, *) {
measure(metrics: [XCTOSSignpostMetric.applicationLaunch]) {
XCUIApplication().launch()
}
}
}
}