Search code examples
objective-ccore-dataios9ios10

NSManagedObjectContext in ios 10 and ios 9 (core data)


I am having an app which use core data which supports both ios 9 as well ios 10.I am using xcode 8.2.1 for developing this project. The AppDelegate file have method for to get ManagedObjectContext when app runs on ios 10 but, how do i get it when app works on ios 9. I tried to google but nothing helped much. Any help !

the code i use to get ManagedObjectContext is here :

 NSManagedObjectContext *context = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).persistentContainer.viewContext;

Solution

  • As Jon Rose mentioned, NSPersistentContainer is only available from iOS 10. Best solution if you still need your app working in previous versions is use the old Core Data Stack, here you have an excellent blog explaining the differences between both methods: Core Data in ios10

    The other solution would be use the #available attribute in swift to execute the Core Data Stack or the NSPersistentContainer, here you have the documentation about this: Swift Attributes