nextseto
nextseto

Reputation: 21

Core Data's NSPersistentContainer in iOS 10 / macOS Sierra

I am really excited to use Core Data now because of how much simpler they made it (as of WWDC 2016).

On app launch, I plan to load pre-updated data from Core Data and then have the persistentContainer's performBackgroundTask do updates/saves to some entities. As entities are updated in the background, the UI should always show the most recent data when fetching (whether those entities were updated or not).

When I'm working with ONE context, is it safe to both set the viewContext's automaticallyMergesChangesFromParent flag to true AND its generation to .current?

lazy var persistentContainer: NSPersistentContainer = {

    let container: NSPersistentContainer = {
        $0.viewContext.automaticallyMergesChangesFromParent = true
        try? $0.viewContext.setQueryGenerationFrom(.current)
        return $0
    }(NSPersistentContainer(name: "MyFirstApp"))

    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error {
            fatalError("Unresolved error \(error), \(error._userInfo)")
        }
    })
    return container
}()

Upvotes: 2

Views: 1267

Answers (1)

Jerry
Jerry

Reputation: 4480

Both those settings should be fine, but if you're only working with one context, neither will matter. automaticallyMergesChangesFromParent will only merge changes when a different context performs a save and query generations are useful when another context is changing data that your current context is reading.

Upvotes: 0

Related Questions