ios - 后台线程崩溃
问题描述
当我在后台模式下获取应用程序时,应用程序崩溃并显示以下日志。
这是崩溃的设备日志:
Exception Type: EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Termination Reason: Namespace ASSERTIOND, Code 0x8badf00d
Triggered by Thread: 0
Filtered syslog:
None found
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x0000000183563de8 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x0000000183563c60 mach_msg + 72
2 CoreFoundation 0x0000000183aa6e40 __CFRunLoopServiceMachPort + 196
3 CoreFoundation 0x0000000183aa4908 __CFRunLoopRun + 1568
4 CoreFoundation 0x00000001839c4da8 CFRunLoopRunSpecific + 552
5 GraphicsServices 0x00000001859aa020 GSEventRunModal + 100
6 UIKit 0x000000018d9e4758 UIApplicationMain + 236
7 AijouNetto 0x00000001008a851c main + 410908 (AppDelegate.swift:17)
8 libdyld.dylib 0x0000000183455fc0 start + 4
这是类实现:
class EKNBackgroundTaskManager {
let backgroundDQ = DispatchQueue.global(qos: .background)
var backgroundUpdateTask: UIBackgroundTaskIdentifier!
init(withName: String) {
self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(withName: withName) {}
}
func runBackgroundTask(withCode: @escaping (_ cH: @escaping () -> Void) -> Void)
{
backgroundDQ.async {
withCode() {
self.endBackgroungTask()
}
}
}
func endBackgroungTask() {
if backgroundUpdateTask != nil && backgroundUpdateTask != UIBackgroundTaskInvalid {
UIApplication.shared.endBackgroundTask(backgroundUpdateTask)
backgroundUpdateTask = UIBackgroundTaskInvalid
}
}
}
有什么建议可以在应用程序进入后台模式时修复此错误?
解决方案
您的应用程序在后台使用了太多时间或启动时间太长,因此被 iOS 看门狗进程终止(仅在设备上发生)。这可以从终止原因代码中看出。有关更多信息,请参阅这篇文章:8badf00d 是什么意思?
您应该检查导致崩溃的原因是开始时间还是后台任务。如果是开始时间,您必须以某种方式减少应用程序的启动时间。Apple 已经就这个主题进行了多次讨论,相应的视频可以在 Apple Developer 平台上找到(在过去几年的 WWDC 视频中,例如:https ://developer.apple.com/videos/play/ WWDC2016/406)。
有关长时间运行的后台任务以及如何实现它们,请参阅 Apple 文档:https ://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc /uid/TP40007072-CH4-SW3
推荐阅读
- javascript - 使用 Javascript 的 POST 方法在 Servlet 中获取 JSON 数据不起作用
- c# - 如何在 ASP.NET MVC 中使用多个提交按钮
- python-3.x - 如何使用python读取自动发送到手机的OTP?
- android - 使用 base64 的 WebView 不加载图像
- vbscript - 是否可以从alm重命名下载的附件?
- postgresql - 将多个 csv.gz 格式导入 windows 上的 postgres 表
- python - 为什么我的 sigmoid 函数返回的值不在 ]0,1[ 区间内?
- docker - 昨天成功运行的 Docker 命令和设置今天开始在 Windows 10 上突然出现错误“ENOENT”
- javascript - 什么是在谷歌地图 API 中创建三角形坐标的理想方法,其中两个位置点由纬度和经度组成
- css - 使 thead 固定且 tbody 可滚动后,thead 和 tbody 未正确对齐