App crashes randomly on production when trying to save or update a record.
It's a VOIP app, getting background CallKit pushes and on some conditions, writes them so CoreDate DB. I suspect that that's what's crashing the app but I could not find any reference to it online.
Tried reproducing this issue locally with no luck, could be because it's impossible to debug with Xcode before you unlock your phone for the first time.
This is my CoreDate code from AppDelegate:
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)
}
}
}
The function where the crash happens:
Call.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)
}
}
Running - Swift 5 - Xcode 10.2