How to instantiate an NSManagedObjectContext in iOS 9

Prior to iOS 9, Apple would do the heavy lifting and setup a Core Data stack with certain templates. One of those methods was the custom initialiser for the managed object context in the AppDelegate.m file.

Since iOS 9 however, the standard init method is deprecated and we need to use a designated initialiser instead, called initWithConcurrency. This initialiser has been around since iOS 5, but up until now it was optional to use it.

Here’s what a typical custom initialiser for a managed object context looked like prior to iOS 9:

Opening a project with a method like this now will give us a deprecation warning and we must use initWithConcurrency instead.

This means that we now have to make a choice about what thread our managed object context is initialised on: the main queue, or perhaps a special background queue we’ve created. Our choices are:

  • NSPrivateQueueConcurrencyType
  • NSMainQueueConcurrencyType

Unless you have a private queue, the previous default behaviour can be brought back with the latter option. Here’s what the updated method would look like:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.