Skip to content

Commit

Permalink
更新 MetricKitManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Nov 17, 2024
1 parent cbf2615 commit 48ac22d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
10 changes: 7 additions & 3 deletions SwiftPamphletApp/App/SwiftPamphletAppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -69,8 +70,8 @@ struct SwiftPamphletAppApp: App {
}

// 任务示例
// TaskCase.bad()
TaskCase.good()
TaskCase.bad()
// TaskCase.good()

// 任务管理器示例
// taskgroupDemo()
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
47 changes: 27 additions & 20 deletions SwiftPamphletApp/Performance/MetricManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
9 changes: 5 additions & 4 deletions SwiftPamphletApp/Performance/TaskManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]()
Expand Down Expand Up @@ -75,7 +75,8 @@ func taskgroupDemo() {
print("Background Task Four Executed")
}

// 开始执行任务
// MARK: 开始执行任务
// 低优先级任务
performLowPriorityTasks(tasks: backgroundTasks)
Task {
await executeTasksConcurrently(tasks: tasks)
Expand Down

0 comments on commit 48ac22d

Please sign in to comment.