MXMetricKit中finishExtendedLaunchMeasurement调用失败的修复咨询
修复MetricKit finishExtendedLaunchMeasurement API报错问题
报错信息
调用
finishExtendedLaunchMeasurementAPI时,Xcode抛出错误:
[General] Couldn't find persisted ALM/FrontBoard launch signpost id when finishing an ext launch task.
错误详情:Error Domain=MXErrorDomain Code=5 "Internal failures happened inside of MetricKit." UserInfo={NSLocalizedDescription=Internal failures happened inside of MetricKit.}
问题代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // create window MXMetricManager.shared.add(self) do { let task_id = MXLaunchTaskID("1234.zmmm") try MXMetricManager.extendLaunchMeasurement(forTaskID: task_id) print("some task perform") try MXMetricManager.finishExtendedLaunchMeasurement(forTaskID: task_id) } catch { print(error) } return true }
修复方案
- 修正任务ID格式:
MXLaunchTaskID必须以应用真实Bundle ID作为前缀,格式为[Bundle ID].[自定义任务名]。比如你的应用Bundle ID是com.example.MyApp,任务ID可以设为MXLaunchTaskID("com.example.MyApp.initSetup"),不能用随意的字符串如1234.zmmm。 - 调整调用时机与方式:不要在
didFinishLaunchingWithOptions中同步连续调用extendLaunchMeasurement和finishExtendedLaunchMeasurement。系统需要时间持久化任务标识,建议将耗时任务放在异步队列执行,或者延迟到窗口完成初始化后再调用,示例:DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { do { let taskId = MXLaunchTaskID("com.example.MyApp.initSetup") try MXMetricManager.extendLaunchMeasurement(forTaskID: taskId) // 执行实际的初始化任务 self.performSetupTask() try MXMetricManager.finishExtendedLaunchMeasurement(forTaskID: taskId) } catch { print(error) } } - 确认协议实现:确保当前类已经实现
MXMetricManagerSubscriber协议的didReceive方法,否则MetricManager无法正确订阅指标,可能影响任务标识的持久化。 - 使用真机测试:MetricKit的扩展启动测量API在iOS模拟器上存在兼容性问题,所有测试请使用真实iOS设备。
内容的提问来源于stack exchange,提问作者moxacist
相关产品推荐
相关产品推荐

