MetricKit框架詳細解析(十一) —— 基於MetricKit的App中的電源監控,性能和診斷(二) 版本記錄 前言 源碼 後記

版本記錄

版本號 時間
V1.0 2021.05.17 星期一

前言

MetricKit由iOS13系統進引入,用來彙總和分析有關異常和崩潰診斷以及電源和性能指標的每個設備的報告。下面我們就一起來看下這個框架。感興趣的可以看下面幾篇文章。
1. MetricKit框架詳細解析(一) —— 基本概覽(一)
2. MetricKit框架詳細解析(二) —— Improving Your App's Performance(一)
3. MetricKit框架詳細解析(三) —— Reducing Your App's Memory Use(一)
4. MetricKit框架詳細解析(四) —— Gathering Information About Memory Use(一)
5. MetricKit框架詳細解析(五) —— Making Changes to Reduce Memory Use(一)
6. MetricKit框架詳細解析(六) —— Preventing Memory-Use Regressions & Responding to Low-Memory Warnings(一)
7. MetricKit框架詳細解析(七) —— Reducing Your App's Launch Time(一)
8. MetricKit框架詳細解析(八) —— Reducing Disk Writes(一)
9. MetricKit框架詳細解析(九) —— Improving App Responsiveness(一)
10. MetricKit框架詳細解析(十) —— 基於MetricKit的App中的電源監控,性能和診斷(一)

源碼

1. Swift

首先看下工程組織結構:

下面就是源碼了

1. AppDelegate.swift
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
}
2. ShoppingListTableViewController.swift
import UIKit
import MetricKit

class ShoppingListTableViewController: UITableViewController {
  let fruit = ["🍏 Apples", "🍌 Bananas", "🍓 Strawberries"]
    let fruitsLogHandle = MXMetricManager.makeLogHandle(category: "Fruits")

  override func viewDidLoad() {
    super.viewDidLoad()

    let metricManager = MXMetricManager.shared
    metricManager.add(self)

        mxSignpost(.event, log: fruitsLogHandle, name: "Loading Fruits TableViewController")
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = fruit[indexPath.row]
    return cell
  }

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return fruit.count
  }
}

extension ShoppingListTableViewController: MXMetricManagerSubscriber {
  func didReceive(_ payloads: [MXMetricPayload]) {
    guard let firstPayload = payloads.first else { return }
        print(firstPayload.dictionaryRepresentation())
    }

    func didReceive(_ payloads: [MXDiagnosticPayload]) {
        guard let firstPayload = payloads.first else { return }
        print(firstPayload.dictionaryRepresentation())
    }
}

後記

本篇主要講述了基於MetricKit的App中的電源監控,性能和診斷,感興趣的給個贊或者關注~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章