【发布时间】:2019-11-12 22:14:18
【问题描述】:
在尝试保存或更新记录时,应用程序在生产中随机崩溃。
这是一个 VOIP 应用程序,获取后台 CallKit 推送,并且在某些情况下,将它们写入 CoreDate DB。我怀疑这就是导致应用程序崩溃的原因,但我在网上找不到任何关于它的参考资料。
尝试在本地重现此问题但没有成功,可能是因为在您第一次解锁手机之前无法使用 Xcode 进行调试。
这是我来自 AppDelegate 的 CoreDate 代码:
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Model")
var persistentStoreDescriptions: NSPersistentStoreDescription
let storeUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.appname")!.appendingPathComponent("Model.sqlite")
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
description.url = storeUrl
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.appname")!.appendingPathComponent("Model.sqlite"))]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
Crashlytics.sharedInstance().recordError(error)
#if DEBUG
fatalError("Unresolved error \(error), \(error.userInfo)")
#endif
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let context = persistentContainer.viewContext
managedContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Crashlytics.sharedInstance().recordError(error)
}
}
}
发生崩溃的函数:
调用.swift:
let callEntity = NSEntityDescription.entity(forEntityName: "Call", in: managedContext)!
static func upsertCall(call: Call?) {
if(call == nil){
return
}
//validation here..
//..
do {
managedContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
try managedContext.save()
} catch {
let nserror = error as NSError
NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
Crashlytics.sharedInstance().recordError(error)
}
}
跑步 - 斯威夫特 5 - Xcode 10.2
【问题讨论】:
-
您好,您的问题解决了吗。