如何为 NSPersistentContainer 和 NSPersistentCloudKitContainer 创建同名但根据版本设置的变量?
Posted
技术标签:
【中文标题】如何为 NSPersistentContainer 和 NSPersistentCloudKitContainer 创建同名但根据版本设置的变量?【英文标题】:How do I create a variable for NSPersistentContainer and NSPersistentCloudKitContainer with the same name but set depending on the version? 【发布时间】:2019-08-05 20:43:03 【问题描述】:我无法将令人兴奋的核心数据堆栈更改为 NSPersistentCloudKitContainer,因为这只是 ios 13 及更高版本的支持功能,但我希望为 iOS 13 及更高版本设置该功能,同时为早期版本设置 NSPersistentContainer。
我在使用 swift 和 iOS 方面拥有丰富的经验,但从来没有为版本控制和某些 var 做这样的事情。
这是我在使用 iOS 13 之前的正常操作:
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: "Shopping_App")
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
()
这是我想在 iOS 13 或更高版本的任何设备上使用的,但仍然使用 iOS 12 及更早版本的第一段代码。
lazy var persistentContainer: NSPersistentCloudKitContainer =
let container = NSPersistentCloudKitContainer(name: "Shopping_App")
container.loadPersistentStores(completionHandler:
(storeDescription, error) in
if let error = error as NSError?
fatalError("Unresolved error \(error), \(error.userInfo)")
)
return container
()
如果可能的话,我希望两个版本具有相同的变量,而不会重新声明错误,如果这甚至可能的话,如果那不可能,我想要一个不需要完全重写我的所有内容的解决方案存储和检索核心数据,data。
【问题讨论】:
【参考方案1】:我能够找到答案,结果非常简单。这里是:
lazy var persistentContainer: NSPersistentContainer =
if #available(iOS 13.0, *)
let container = NSPersistentCloudKitContainer(name: "Shopping_App")
container.loadPersistentStores(completionHandler:
(storeDescription, error) in
if let error = error as NSError?
fatalError("Unresolved error \(error), \(error.userInfo)")
)
return container
else
let container = NSPersistentContainer(name: "Shopping_App")
container.loadPersistentStores(completionHandler: (storeDescription, error) in
if let error = error as NSError?
fatalError("Unresolved error \(error), \(error.userInfo)")
)
return container
()
【讨论】:
以上是关于如何为 NSPersistentContainer 和 NSPersistentCloudKitContainer 创建同名但根据版本设置的变量?的主要内容,如果未能解决你的问题,请参考以下文章
NSPersistentContainer 不创建数据库文件
NSPersistentContainer / 核心数据 / 只读存储
NSPersistentContainer, performBackgroundTask, 调用 perform 啥都不做
NSPersistentContainer的loadPersistentStores的completionHandler是不是同步运行?