I cannot change my existing core data stack to NSPersistentCloudKitContainer because that is only a iOS 13 and later support feature but I would like to have that set for iOS 13 and later while having NSPersistentContainer for earlier versions.
I have a decent amount of experience working with swift and iOS but have not ever had to do something like this for versioning and certain vars/lets on certain verions.
This is my code for my coredata stack for the iOS 13 and later for NSPersistentCloudKitContainer:
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
}()This is my code for iOS 12 and earlier:
lazy var persistentContainer: NSPersistentContainer = {
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
}()
How could I keep both of these but have the first for iOS 13 and later and the second for iOS 12 and earlier? I would like to have one var for both if that is even possible and if that is not possible I would like a easy solution that makes it so I do not have to rewrite all that I have already to include two different versions and vars.
Why don't you use `if #available` ?
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
}
}()