This is how I create Core Data Stack.
Code Block swift class AppGroupLocalPersistentContainer: NSPersistentContainer { open override class func defaultDirectoryURL() -> URL { let storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.mindo") return storeURL! } } extension NSPersistentContainer { var url: URL? { let url: URL? = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.mindo") return url?.appendingPathComponent("Mindo.sqlite") } } public class LocalCoreData { deinit { Logger.info("LocalCoreData deinit") } lazy var localContext: NSManagedObjectContext = { let context = localContainer.viewContext context.mergePolicy = NSMergePolicy(merge: NSMergePolicyType.mergeByPropertyObjectTrumpMergePolicyType) context.automaticallyMergesChangesFromParent = true context.stalenessInterval = 0 return context }() lazy var localContainer: NSPersistentContainer = { let container = AppGroupLocalPersistentContainer(name: "Mindo", managedObjectModel: NSManagedObjectModel.mergedModel(from: [Bundle.main])!) guard let description = container.persistentStoreDescriptions.first else { fatalError("Failed to retrieve a persistent store description.") } description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(false as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) description.shouldMigrateStoreAutomatically = true description.shouldInferMappingModelAutomatically = true container.loadPersistentStores(completionHandler: { _, error in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) return container }() }
And here is part of crash log I copy from Xcode Organizer.
Code Block Thread 0 name: Thread 0 Crashed: 0 libswiftCore.dylib 0x0000000187d45cb4 _assertionFailure(_:_:file:line:flags:) + 492 (AssertCommon.swift:132) 1 libswiftCore.dylib 0x0000000187d45cb4 _assertionFailure(_:_:file:line:flags:) + 492 (AssertCommon.swift:132) 2 Mindo 0x0000000104245088 closure #1 in closure #1 in LocalCoreData.localContainer.getter + 604 (LocalCoreData.swift:39) 3 Mindo 0x00000001042449a0 thunk for @escaping @callee_guaranteed (@guaranteed NSPersistentStoreDescription, @guaranteed Error?) -> () + 64 (<compiler-generated>:0) 4 CoreData 0x000000018a3da570 -[NSPersistentStoreCoordinator _doAddPersistentStoreWithDescription:privateCopy:completeOnMainThread:withHandler:] + 1008 (NSPersistentStoreCoordinator.m:1461) 5 CoreData 0x000000018a28146c -[NSPersistentStoreCoordinator addPersistentStoreWithDescription:completionHandler:] + 268 (NSPersistentStoreCoordinator.m:1507) 6 CoreData 0x000000018a4ae880 -[NSPersistentContainer _loadStoreDescriptions:withCompletionHandler:] + 232 (NSPersistentContainer.m:284) 7 CoreData 0x000000018a289658 -[NSPersistentContainer loadPersistentStoresWithCompletionHandler:] + 316 (NSPersistentContainer.m:271) 8 Mindo 0x0000000104244d10 closure #1 in LocalCoreData.localContainer.getter + 848 (LocalCoreData.swift:36)
I know the error is occur in this line :LocalCoreData.swift:39
Code Block fatalError("Unresolved error \(error), \(error.userInfo)")
But I have no idea how to fix this.