I would like to know if just saving a background context like in the below example is enough or I should also save the main context and why.
extension NSManagedObjectContext {
private func saveOrRollback(context: NSManagedObjectContext) -> Bool {
do {
if context.hasChanges {
try context.save()
return true
}
} catch {
log(error, message: "Couldn't save. Rolling back.", tag: .coreData)
context.rollback()
return false
}
return false
}
func performChanges(block: @escaping () -> Void) {
let privateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
privateContext.parent = self
privateContext.perform {
block()
_ = self.saveOrRollback(context: privateContext)
}
}
}