首页 > 解决方案 > 了解 MetricKit...didRecieve?

问题描述

我最近将 MetricKit 添加到我的应用程序中,并且我已经完成了研究,并认为我通过使用文档示例了解了它是如何工作的。我已经添加了 MetricManager 共享对象以及 require 协议。我在文档中阅读此内容的方式是,我的应用程序将提供每天(每 24 小时)的指标,但我的问题是,“如何在 Xcode Organizer 中读取从应用程序接收到的指标?”

我已经使用 MetricKit 对象设置了我的应用程序并在应用程序委托中对其进行了实例化,但在过去的 6 个月中我从未收到任何指标。我知道在 didRecieve(_ payloads: ) 方法中,我可以将这些有效负载发送到另一个 Web 服务器,如 Vapor 或其他服务,但我仍然不明白这些指标是如何到达 Organizer 的。如果我检查有效负载并使用 Simulate MetricKit Payload 打印 payloads.dictionaryRepresentation(),则会按预期调用 didRecieve。

如果我只想让 Metrics 显示在我的应用程序的 Xcode Organizer 中,我是否只是将 didRecieve 方法留空?这是自动的吗?就这么简单吗?

Xcode 12.5 和 Swift 5

我的指标对象

import MetricKit

class AppMetrics: NSObject, MXMetricManagerSubscriber {
  func receiveReports() {
    let shared = MXMetricManager.shared
    shared.add(self)
  }

  func pauseReports() {
    let shared = MXMetricManager.shared
    shared.remove(self)
  }

  func didReceive(_ payloads: [MXMetricPayload]) {

    //What goes here if I just want to see the metrics on Xcode Organizer for my app????
    //Do I just leave it blank and it will automatically get sent to the organizer for my app?
   
  }

  @available(iOS 14.0, *)
  func didReceive(_ payloads: [MXDiagnosticPayload]) {

   //Same issue here with the new Diagnostic Payload?  What do I put to display in the Organizer?

  }
}

在我的 AppDelegate

import UIKit
import MetricKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  let appMetrics = AppMetrics()

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    appMetrics.receiveReports()
    return true
  }

  func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    
  }

  func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    
  }

  func applicationWillTerminate(_ application: UIApplication) {

    appMetrics.pauseReports()

  }

}

标签: iosswiftmetrics

解决方案


经过进一步研究,答案似乎是,如果我要将指标上传到我自己的服务器或另一台服务器,我只需要设置 MetricKit。显然,苹果每 24 小时向 Xcode Organizer 提供应用程序指标应该是自动的。出于某种原因,我根本没有收到任何让我认为我做错了什么的指标。问题出在苹果的某个地方。因此,如果 Xcode Organizer Metrics 继续不显示,最好将指标设置到 API 并在那里查看它们,例如 Firebase Performance 或 SwiftInfo。希望这对其他人进行研究时有所帮助。

这是一个很好的参考: https ://medium.com/expedia-group-tech/monitoring-app-performance-on-ios-7a48fb25cfc2


推荐阅读