gpt4 book ai didi

core-data - 如何解决: "NSPersistentStoreCoordinator has no persistent stores"?

转载 作者:行者123 更新时间:2023-12-02 21:35:09 37 4
gpt4 key购买 nike

我完全按照本教程进行操作,它将 CoreData 添加到现有应用程序中: https://www.youtube.com/watch?v=WcQkBYu86h8

当我到达 seedPerson() moc.save() 时,应用程序崩溃并出现以下错误:

CoreData: error: Illegal attempt to save to a file that was never opened. "This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.". No last error recorded.

已添加 NSManagedSubclass。

DataController 已连接好,我可以进入其中。直到 save() 才出现问题。知道我可能遗漏了什么导致此错误吗?

最佳答案

我也遵循了 YouTube 教程,但遇到了同样的问题。我刚刚删除了添加持久存储的后台线程 block ,它起作用了。这是我的DataController:

import UIKit
import CoreData

class WellbetDataController: NSObject {
var managedObjectContext: NSManagedObjectContext

override init() {
// This resource is the same name as your xcdatamodeld contained in your project.
guard let modelURL = NSBundle.mainBundle().URLForResource("DataModel", withExtension:"momd") else {
fatalError("Error loading model from bundle")
}

// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
guard let mom = NSManagedObjectModel(contentsOfURL: modelURL) else {
fatalError("Error initializing mom from: \(modelURL)")
}

let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
self.managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
self.managedObjectContext.persistentStoreCoordinator = psc

let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let docURL = urls[urls.endIndex-1]
/* The directory the application uses to store the Core Data store file.
This code uses a file named "DataModel.sqlite" in the application's documents directory.
*/
let storeURL = docURL.URLByAppendingPathComponent("DataModel.sqlite")
do {
try psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil)
} catch {
fatalError("Error migrating store: \(error)")
}



// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
// let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
// let docURL = urls[urls.endIndex-1]
// /* The directory the application uses to store the Core Data store file.
// This code uses a file named "DataModel.sqlite" in the application's documents directory.
// */
// let storeURL = docURL.URLByAppendingPathComponent("DataModel.sqlite")
// do {
// try psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil)
// } catch {
// fatalError("Error migrating store: \(error)")
// }
// }
}
}

关于core-data - 如何解决: "NSPersistentStoreCoordinator has no persistent stores"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332097/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com