Chapter7. Why did you say you made newBackgroundContext as a parent, but actually took another action in code?

You wrote " When using a single managed object context in Core Data, everything runs on the main UI thread. However, this project has a separate private queue context which acts as the root context, letting you save data on a background thread. The main context exposed by the Core Data stack is a child of this context." Great.
But why did made opposite steps in code provided to me???
import Foundation
import CoreData

 open class CoreDataStack {

  let modelName: String

 public init(modelName: String) {
self.modelName = modelName
 }

  public lazy var mainContext: NSManagedObjectContext = {
   return self.storeContainer.viewContext
 }()

  public lazy var storeContainer: NSPersistentContainer = {

let container = NSPersistentContainer(name: self.modelName)
container.loadPersistentStores { (storeDescription, error) in
  if let error = error as? NSError {
    fatalError("Unresolved error \(error), \(error.userInfo)")
  }
}
  return container
  }()

 public func newDerivedContext() -> NSManagedObjectContext {
let context = storeContainer.newBackgroundContext()
return context
 }

  public func saveContext() {
saveContext(mainContext)

}

 public func saveContext(_ context: NSManagedObjectContext) {
if context != mainContext {
  saveDerivedContext(context)
  return
}

context.perform {
  do {
    try context.save()
  } catch let nserror as NSError {
    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
  }
}
}
public func saveDerivedContext(_ context: NSManagedObjectContext) {
context.perform {
  do {
    try context.save()
  } catch let nserror as NSError {
    fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
  }

  self.saveContext(self.mainContext)
}
 }
}

Here. you performed save action like it is mainContext is parent and backgroundContext is a child. Why??? Please answer. do not ignore this question. Thank you.
public func saveDerivedContext( context: NSManagedObjectContext) {_
_ context.perform {_
_ do {_
_ try context.save()_
_ } catch let nserror as NSError {_
_ fatalError(“Unresolved error (nserror), (nserror.userInfo)”)_
_ }_

_ self.saveContext(self.mainContext)_
_ }_
_ }_
}

@astralbodies Do you have any feedback about this? Thank you - much appreciated! :]

Guys whats going on? Please respond to my question. Why do you ignore my question? Whats going on???

Hi,

There’s possibly a hangover in the text here from where we were building the persistent store coordinator in a different way in the previous versions (we moved to NSPersistentContainer for this edition).

However, any background context given to the app for performing work will still be a child of the main context, and will push changes up to the main context when it is saved.

If a core data stack is using a private context for saving this will always be private to the stack itself, and any worker background contexts will be saved in the same way.

I’ll update the text to make it clearer for the next edition.

Thanks again for your feedback on the book.

You pointed out an error in the text, these things happen, we are only human. We update the book every year which is a large amount of work, and sometimes mistakes creep in.

The mistake has been corrected and will be available to all readers, including you, for free, when the book is next updated.

None of this constitutes a “horrible attitude to readers”. We care deeply about our readers, as evidenced by the fact that we run these forums, respond to questions, and provide free updates to our books every year. Please use a less aggressive tone in your future forum posts, you will find that you get much more positive responses that way :slight_smile:

2 Likes

This topic was automatically closed after 166 days. New replies are no longer allowed.