Updating Items

With the Core Data stack complete, you can now interact with it. Primarily, you will do this through its mainQueueContext. This is how you will both create new entities and save changes.

Inserting into the context

When an entity is created, it should be inserted into a managed object context.

Open FlickrAPI.swift and import CoreData.

import Foundation
import CoreData

Then update the photoFromJSONObject(_:) method to take in an additional argument of type NSManagedObjectContext. It will then use this context to insert new Photo instances.

private static func photoFromJSONObject(json: [String : AnyObject],
        inContext context: NSManagedObjectContext) -> Photo? { guard let photoID = json["id"] as? String, ...

Get iOS Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.