From 48ac22d2077dcaa1b4c5f8429b9f2c1dd60674ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E9=93=AD?= Date: Sun, 17 Nov 2024 20:43:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20MetricKitManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../App/SwiftPamphletAppApp.swift | 10 ++-- .../Performance/MetricManager.swift | 47 +++++++++++-------- .../Performance/TaskManager.swift | 9 ++-- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/SwiftPamphletApp/App/SwiftPamphletAppApp.swift b/SwiftPamphletApp/App/SwiftPamphletAppApp.swift index 7a4a68b4..c258ea79 100644 --- a/SwiftPamphletApp/App/SwiftPamphletAppApp.swift +++ b/SwiftPamphletApp/App/SwiftPamphletAppApp.swift @@ -17,6 +17,7 @@ import AppIntents @main struct SwiftPamphletAppApp: App { + @State private var metricsManager = MetricsManager() // 启动时间打点 private let launchStartTime = DispatchTime.now() private let signpostID = OSSignpostID(log: OSLog.default) @@ -69,8 +70,8 @@ struct SwiftPamphletAppApp: App { } // 任务示例 -// TaskCase.bad() - TaskCase.good() + TaskCase.bad() +// TaskCase.good() // 任务管理器示例 // taskgroupDemo() @@ -92,6 +93,10 @@ struct SwiftPamphletAppApp: App { break } } + .environment(metricsManager) + .task { + await metricsManager.fetchMetrics() + } // .modelContainer(for: [IOInfo.self, DeveloperModel.self, BookmarkModel.self], isUndoEnabled: true) #endif } @@ -154,7 +159,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { #elseif os(iOS) class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { - _ = MetricKitManager.shared // 上报启动时间 print("didFinishLaunchingWithOptions") return true } diff --git a/SwiftPamphletApp/Performance/MetricManager.swift b/SwiftPamphletApp/Performance/MetricManager.swift index 1abef16e..af17ee60 100644 --- a/SwiftPamphletApp/Performance/MetricManager.swift +++ b/SwiftPamphletApp/Performance/MetricManager.swift @@ -6,38 +6,45 @@ // import MetricKit +import Observation -@objc class MetricKitManager: NSObject, MXMetricManagerSubscriber { - @MainActor @objc public static let shared = MetricKitManager() +@Observable +@MainActor +class MetricsManager: NSObject, @preconcurrency MXMetricManagerSubscriber { + var latestPayload: MXMetricPayload? - private override init() { + override init() { super.init() - start() + MXMetricManager.shared.add(self) } - func start() { - let metricManager = MXMetricManager.shared - metricManager.add(self) - } - deinit { MXMetricManager.shared.remove(self) } -} -extension MetricKitManager { - #if os(iOS) - @available(iOS 13.0, *) func didReceive(_ payloads: [MXMetricPayload]) { - // 获取启动时间数据 - guard let firstPayload = payloads.first else { return } - print("Launch Time Data: \(firstPayload.dictionaryRepresentation())") + Task { @MainActor in + if let latest = payloads.last { + self.latestPayload = latest + } + } } - #endif - @available(iOS 14.0, *) func didReceive(_ payloads: [MXDiagnosticPayload]) { - guard let firstPayload = payloads.first else { return } - print("Diagnostic Data: \(firstPayload.dictionaryRepresentation())") + // Handle diagnostic payloads if needed + } + + func fetchMetrics() async { + // Example async function to fetch metrics + try! await Task.sleep(nanoseconds: 2 * 1_000_000_000) // Simulate network delay + // Update state with fetched metrics + } +} + +extension MetricsManager { + func performAsyncOperation() async { + await Task { @Sendable in + // Perform some async operation + }.value } } diff --git a/SwiftPamphletApp/Performance/TaskManager.swift b/SwiftPamphletApp/Performance/TaskManager.swift index c9c1d175..5564d44d 100644 --- a/SwiftPamphletApp/Performance/TaskManager.swift +++ b/SwiftPamphletApp/Performance/TaskManager.swift @@ -19,8 +19,8 @@ func executeTasksConcurrently(tasks: [@Sendable () async -> Void]) async { } } -// 执行低优先级任务 -func performLowPriorityTasks(tasks: [@Sendable () async -> Void]) { +// 执行低优先级异步任务 +func performLowPriorityTasks(tasks: [@Sendable () async -> Void], withTimeLimit: Double? = nil) { for task in tasks { Task.detached(priority: .background) { await task() @@ -30,7 +30,7 @@ func performLowPriorityTasks(tasks: [@Sendable () async -> Void]) { func taskgroupDemo() { @Sendable func wait() async { -// try? await Task.sleep(nanoseconds: 1_000_000_000) + try? await Task.sleep(nanoseconds: 2_000_000_000) } var tasks = [@Sendable () async -> Void]() @@ -75,7 +75,8 @@ func taskgroupDemo() { print("Background Task Four Executed") } - // 开始执行任务 + // MARK: 开始执行任务 + // 低优先级任务 performLowPriorityTasks(tasks: backgroundTasks) Task { await executeTasksConcurrently(tasks: tasks)