在进行CoreData的开发时,有时你需要知道数据库里到底发生了什么变化,这时就需要了解到CoreData数据到底存到了哪里。这是Xcode自动生成的CoreData初始化部分的代码:
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "testCoreData")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
}
在这里,可以通过print storeDescription即可得到存储路径
init(inMemory: Bool = false) {
container = NSPersistentContainer(name: "testCoreData")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
// show storePath
print(storeDescription)
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
}