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

**URL:** <https://forums.kodeco.com/t/chapter7-why-did-you-say-you-made-newbackgroundcontext-as-a-parent-but-actually-took-another-action-in-code/37079>\
**Category:** Core Data by Tutorials\
**Created:** [November 28, 2017, 2:14pm UTC](https://forums.kodeco.com/t/chapter7-why-did-you-say-you-made-newbackgroundcontext-as-a-parent-but-actually-took-another-action-in-code/37079 "2017-11-28T14:14:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![simons1994](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/simons1994/32/81494_2.png) [@simons1994](https://forums.kodeco.com/u/simons1994)\
**Post date:** [November 28, 2017, 2:14pm UTC](https://forums.kodeco.com/t/chapter7-why-did-you-say-you-made-newbackgroundcontext-as-a-parent-but-actually-took-another-action-in-code/37079/1 "2017-11-28T14:14:24Z")

</div>

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)\_  
\_ }\_  
\_ }\_  
_}_

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 29, 2017, 12:11pm UTC](https://forums.kodeco.com/t/chapter7-why-did-you-say-you-made-newbackgroundcontext-as-a-parent-but-actually-took-another-action-in-code/37079/2 "2017-11-29T12:11:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![simons1994](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/simons1994/32/81494_2.png) [@simons1994](https://forums.kodeco.com/u/simons1994)\
**Post date:** [December 4, 2017, 4:06pm UTC](https://forums.kodeco.com/t/chapter7-why-did-you-say-you-made-newbackgroundcontext-as-a-parent-but-actually-took-another-action-in-code/37079/3 "2017-12-04T16:06:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jrturton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jrturton/32/109667_2.png) [@jrturton](https://forums.kodeco.com/u/jrturton)\
**Post date:** [December 4, 2017, 6:26pm UTC](https://forums.kodeco.com/t/chapter7-why-did-you-say-you-made-newbackgroundcontext-as-a-parent-but-actually-took-another-action-in-code/37079/4 "2017-12-04T18:26:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jrturton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jrturton/32/109667_2.png) [@jrturton](https://forums.kodeco.com/u/jrturton)\
**Post date:** [December 5, 2017, 2:08pm UTC](https://forums.kodeco.com/t/chapter7-why-did-you-say-you-made-newbackgroundcontext-as-a-parent-but-actually-took-another-action-in-code/37079/6 "2017-12-05T14:08:51Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [May 14, 2018, 6:14am UTC](https://forums.kodeco.com/t/chapter7-why-did-you-say-you-made-newbackgroundcontext-as-a-parent-but-actually-took-another-action-in-code/37079/7 "2018-05-14T06:14:26Z")

</div>

Closed on May 14, 2018, 6:14am UTC

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