title: performBackgroundTask not saving when app in background Swift
body: <p>I'm using Xcode 9 and Swift 4. I start a download of multiple JSON files when the application is in the foreground. The application then parses these files and saves them to CoreData. This works well when the application is in the foreground. However, if the application is in the background, the files still download correctly, but the data is not parsed and saved to CoreData. It's only when the user returns to the foreground that the parsing and saving of data continues.</p>
  
  <p>I have Background Modes turned on - Background Fetch and Remote notifications.</p>
  
  <p>I have around 10 functions that are similar to the one below in which it processes the JSON files concurrently:</p>
  
  <pre><code>func populateStuff(json: JSONDictionary) -&gt; Void {
      let results = json["result"] as! JSONDictionary
      let stuffData = results["Stuff"] as! [JSONDictionary]
  
      let persistentContainer = getPersistentContainer()
  
      persistentContainer.performBackgroundTask { (context) in
          for stuff in stuffData {
              let newStuff = Stuff(context: context)
              newStuff.stuffCode = stuff["Code"] as? String
              newStuff.stuffDescription = stuff["Description"] as? String
  
              do {
                  try context.save()
              } catch {
                  fatalError("Failure to save context: \(error)")
              }
          }
      }
  }
  
  func getPersistentContainer() -&gt; NSPersistentContainer {
      let persistentContainer = NSPersistentContainer(name: "MyProjectName")
      persistentContainer.loadPersistentStores { (_, error) in
          if let error = error {
              fatalError("Failed to load core data stack: \(error.localizedDescription)")
          }
      }
      persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
      persistentContainer.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
      return persistentContainer
  }
  </code></pre>
  
  <p>Can anyone advise me on why this might happen and how to over come this?</p>
  
  <p>TIA</p>