如何解决 AppDelegate 中的 coreData 错误?
Posted
技术标签:
【中文标题】如何解决 AppDelegate 中的 coreData 错误?【英文标题】:How to solve coreData error in AppDelegate? 【发布时间】:2017-02-25 15:54:09 【问题描述】:其实我在 ios 10 的 coreData 方面有一些经验。 但我现在创建了一个目标是#version 8.0 及更高版本的应用程序。 首先,我创建了一个包含 coreData 的应用程序。所以他们会在 AppDelegate 中自动为我生成一个代码作为 iOS 10 标准。当我将目标 10 更改为 8.0 时,AppDelegate 会显示一些错误。
AppDelegate 中的错误
如何解决这个问题?我想在 ios 8.0 及更高版本中使用 coreData。
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer =
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "coreDataTestForPreOS")
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.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
)
return container
()
// MARK: - Core Data Saving support
func saveContext ()
let context = persistentContainer.viewContext
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.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
let ad = UIApplication.shared.delegate as! AppDelegate
let context = ad.persistentContainer.viewContext
【问题讨论】:
在下面查看我的答案。 【参考方案1】:var context: NSManagedObjectContext?
if #available(iOS 10.0, *)
context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
else
// iOS 9.0 and below - however you were previously handling it
guard let modelURL = Bundle.main.url(forResource: "Model", withExtension:"momd") else
fatalError("Error loading model from bundle")
guard let mom = NSManagedObjectModel(contentsOf: modelURL) else
fatalError("Error initializing mom from: \(modelURL)")
let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let docURL = urls[urls.endIndex-1]
let storeURL = docURL.appendingPathComponent("Model.sqlite")
do
try psc.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: nil)
catch
fatalError("Error migrating store: \(error)")
`
【讨论】:
我在哪里写的?你能编辑我上面发布的代码并重新发布吗 在 saveContext() 中?【参考方案2】:在NSPersistentContainer
方便之前有NSPersistentStoreCoordinator
。您需要创建其中之一:
if
let modelURL = Bundle.main.url(forResource: "Model", withExtension: "momd"),
let model = NSManagedObjectModel(contentsOf: modelURL),
let psc = NSPersistentStoreCoordinator(managedObjectModel: model)
...
然后通过.addPersistentStore(ofType:configurationName:at:options)
创建并添加一个或多个NSPersistentStore
s。
【讨论】:
在哪里?我不明白。我是 iOS 的初学者而不是专家,请简要说明我在哪里添加以及它是如何工作的。以上是关于如何解决 AppDelegate 中的 coreData 错误?的主要内容,如果未能解决你的问题,请参考以下文章
Swiftui: MacOS App: Fetching Core Data in ContentView OK,但是如何使用 AppDelegate 的结果呢?
如何在 AppDelegate 中初始化 Core Data 的持久化容器并在整个应用程序中使用它?
App Delegate 中 Core Data 堆栈变量中的 Swift 语法歧义
如何正确设置 Core Data Stack 到 iOS 和 Swift 中的第一个视图控制器?
在 Core Data 中从 AppDelegate 执行回滚功能时需要访问 ManagedObjectContext 的同一实例