Skip to content

Instantly share code, notes, and snippets.

@frr149
Created May 3, 2016 18:38
Show Gist options
  • Save frr149/febf3a06c40e7de363b192c04dabfef9 to your computer and use it in GitHub Desktop.
Save frr149/febf3a06c40e7de363b192c04dabfef9 to your computer and use it in GitHub Desktop.
// Within the init: Create a background context child of main context
backgroundContext = NSManagedObjectContext(concurrencyType: .
// MARK: - Batch processing in the background
extension CoreDataStack{
typealias Batch=(workerContext: NSManagedObjectContext) -> ()
func performBackgroundBatchOperation(batch: Batch){
backgroundContext.performBlock(){
batch(workerContext: self.backgroundContext)
// Save it to the parent context, so normal saving
// can work
do{
try self.backgroundContext.save()
}catch{
fatalError("Error while saving backgroundContext: \(error)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment