NovaUI 0.1.0

NovaUI 0.1.0

Jayden Liu 维护。



NovaUI 0.1.0

  • Jayden Liu

Nova

语言:英文 | 中文

Nova 是集成到 App 中的性能监控 SDK。您可以在实时中监控 App 中是否出现问题时。主要包括以下功能

开发和测试阶段

  • 问题提醒:在 App 中弹出问题通知,检查问题日志。
    • 内存泄露
    • ANR
    • 子线程更新 UI
  • 浮动窗口:显示 CPU / 内存 / FPS 指示器

生产阶段

  • 问题回调:通过委托回调通知 App。App 可以将这些问题报告给后端。

如何使用

Nova由核心层、UI层和多个独立的插件组成。Nova核心是Nova的核心部分,管理Nova和插件的周期,以及事件分布。Nova UI主要是一个调试工具。负责问题通知弹窗和Nova页面的渲染。建议在所有构建中集成Nova核心,在内部测试构建中集成Nova UI。

Podfile

Nova核心是Nova的默认子规范。只要在Podfile中连接插件,Nova核心就会默认连接。

   pod 'NovaMetrics', subspecs: [
      'AnrMonitorPlugin',
      'PageMonitorPlugin',
      'MemoryLeaksPlugin',
      'UIThreadMonitorPlugin',
   ]

NovaUI是一个调试工具,建议仅在测试构建中集成。可以通过Podfile中的配置进行配置。

   pod 'NovaUI', configurations: %w[Debug]

启动Nova

首先,你需要创建一个插件实例,然后调用Nova的启动接口,并传入插件数组。

public class Nova {
    /// Launch Nova with plugins.
    /// - Parameter plugins: Plugins must be subclasses of NovaPlugin.
    public func launch(plugins: [NovaPlugin])
}

示例

let plugins = [
    AnrMonitorPlugin(),
    MemoryLeakMonitorPlugin(),
    UIThreadMonitorPlugin(),
]
Nova.shared.launch(plugins: plugins)

for plugin in plugins {
    plugin.start()
}

Nova日志

Nova默认不在控制台输出日志。如需获取Nova内部的日志,App首先需要设置Nova的logDelegate属性,然后实现NovaLogDelegate协议。

public class Nova {
    /// Nova does not output logs by default. If you need to get the log information in the Nova, you need to set the logDelegate and implement the corresponding method.
    public weak var logDelegate: NovaLogDelegate?
}

示例

Nova.shared.logDelegate = self
 
extension NovaManager: NovaLogDelegate {
    public func shouldLog(level: NovaLogLevel) -> Bool {
        level == .warn || level == .error
    }
 
    public func novaLog(level: NovaLogLevel, module: String, file: String, function: String, line: Int, message: String) {
        // print log
    }
}

问题监控

应用层可以监控插件是否生成问题,并确定是否报告问题。

public class Nova {
    /// Add Nova listener. For example, when an issue occurs, the `onReport` callback will be called
    /// - Parameter listener: listening instance
    public func addListener(_ listener: NovaDelegate)
}

示例

Nova.shared.addListener(self)
 
extension NovaManager: NovaDelegate {
    public func onReport(_ issue: NovaIssue) {
        reporter.report(issue: issue)
    }
}

启动NovaUI

public struct NovaUIConfig {
    /// If the log is enabled, when an issue occurs, SDK will write the issue log to the local file.
    public var isIssueLogEnabled: Bool
 
    /// If issue notification is enabled, when an issue occurs, an issue notification will pop up in the App.
    public var isIssueNotificationEnabled: Bool
 
    /// If the floating window is enabled, the floating window related to performance indicators (cpu, memory, FPS) will be displayed in the App.
    public var isFloatingWindowEnabled: Bool
 
    public init(isIssueLogEnabled: Bool, isIssueNotificationEnabled: Bool, isFloatingWindowEnabled: Bool)
}
 
public class NovaUI {
    public func launch(defaultConfig: NovaUIConfig)
}

示例

let uiConfig = NovaUIConfig(isIssueLogEnabled: true, isIssueNotificationEnabled: true, isFloatingWindowEnabled: true)
NovaUI.shared.launch(defaultConfig: uiConfig)

浮动窗口

实时显示当前应用性能指标,包括

  • 内存使用
  • CPU使用
  • 帧率信息

浮动窗口的默认开关可以通过NovaUIConfig中的isFloatingWindowEnabled属性来控制。

插件介绍

插件 描述 第三方依赖
AnrMonitorPlugin 监控主线程的运行状态,如果主线程阻塞超过阈值
PageMonitorPlugin 页面创建耗时统计,纯统计插件,不会报告问题 VCProfiler
MemoryLeakMonitorPlugin 观察对象的创建和回收以确定是否存在内存泄漏 MLeaksFinder
UIThreadMonitorPlugin 监控子线程是否操作更新UI