// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →

A Quick Core Data Stack with NSPersistentContainer

Setting up a Core Data stack used to take a lot of boilerplate. In iOS 10 and macOS 10.12, NSPersistentContainer does it in a few lines, with code.

Art+Logic Mar 6, 20172 min read

From the Art+Logic archive · written in 2017. The tools may have moved on; the thinking still holds.

Photo by Markus Spiske on Unsplash

image of guy typing on laptop keyboard with left hand holding coffee in his right handOne of the longstanding criticisms of Core Data is how much code it takes to setup the infamous Core Data stack in your iOS or macOS app just so that you can create some instances of entities and save them to a persistent store. The frustration has spawned a number of blog posts outlining the latest, greatest way to setup your stack. Not long after the blog posts came the open source projects aimed at reducing the amount of boilerplate you’ve got to write and helping you avoid common mistakes.

With the release of iOS 10 and macOS 10.12 in many cases you can now forget about the blog posts and third party dependencies thanks to NSPersistentContainer. This one is a no-brainer and it would have been nice if Apple had included it back in the days of iOS 3.0 when they introduced Core Data.

Getting Started

Ready?

NSPersistentContainer.m

self.persistentContainer = [NSPersistentContainer persistentContainerWithName:@"Model"];
[self.persistentContainer loadPersistentStoresWithCompletionHandler:
 ^(NSPersistentStoreDescription* description, NSError* error) {
   NSLog(@"%@ %@", description, error);
}];

In its most basic form, there’s really not much to it, you simply pass in the name of your Core Data model file and ask the NSPersistentContainer instance to load the related stores. There are also initializers that allow you to specify the NSManagedObjectModel yourself if you’d prefer.

So what does this get you?

If this doesn’t meet your needs, and you need a custom setup, the existing APIs are still available, boilerplate and all.

How About Some Sample Code?

I’ve updated the ALAirports project from my earlier posts about Core Spotlight to use Core Data and create the stack using NSPersistentCoordinator. With a few simple changes to the ALAirport, ALAirportController, and the AppDelegate classes we’re able to import the airport data from the bundled CSV on the first launch of the app, and simply rely on the imported data in the persistent store from then on.


Header image by Frédéric Minne, licensed under Creative Commons.

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…