Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Source/DATAStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import CoreData

private var containerURL = URL.directoryURL()

private let backgroundContextName = "DATAStack.backgroundContextName"

/**
The context for the main queue. Please do not use this to mutate data, use `performInNewBackgroundContext`
instead.
Expand Down Expand Up @@ -232,6 +234,7 @@ import CoreData
*/
@objc public func newBackgroundContext() -> NSManagedObjectContext {
let context = NSManagedObjectContext(concurrencyType: DATAStack.backgroundConcurrencyType())
context.name = backgroundContextName
context.persistentStoreCoordinator = self.persistentStoreCoordinator
context.undoManager = nil
context.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
Expand Down Expand Up @@ -373,6 +376,11 @@ import CoreData

// Can't be private, has to be internal in order to be used as a selector.
@objc func backgroundContextDidSave(_ notification: Notification) throws {
let context = notification.object as? NSManagedObjectContext
guard context?.name == backgroundContextName else {
return
}

if Thread.isMainThread && TestCheck.isTesting == false {
throw NSError(info: "Background context saved in the main thread. Use context's `performBlock`", previousError: nil)
} else {
Expand Down