File tree Expand file tree Collapse file tree 1 file changed +8
-0
lines changed
Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,8 @@ import CoreData
2727
2828 private var containerURL = URL . directoryURL ( )
2929
30+ private let backgroundContextName = " DataStack.backgroundContextName "
31+
3032 /**
3133 The context for the main queue. Please do not use this to mutate data, use `performInNewBackgroundContext`
3234 instead.
@@ -232,6 +234,7 @@ import CoreData
232234 */
233235 @objc public func newBackgroundContext( ) -> NSManagedObjectContext {
234236 let context = NSManagedObjectContext ( concurrencyType: DataStack . backgroundConcurrencyType ( ) )
237+ context. name = backgroundContextName
235238 context. persistentStoreCoordinator = self . persistentStoreCoordinator
236239 context. undoManager = nil
237240 context. mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
@@ -373,6 +376,11 @@ import CoreData
373376
374377 // Can't be private, has to be internal in order to be used as a selector.
375378 @objc func backgroundContextDidSave( _ notification: Notification ) throws {
379+ let context = notification. object as? NSManagedObjectContext
380+ guard context? . name == backgroundContextName else {
381+ return
382+ }
383+
376384 if Thread . isMainThread && TestCheck . isTesting == false {
377385 throw NSError ( info: " Background context saved in the main thread. Use context's `performBlock` " , previousError: nil )
378386 } else {
You can’t perform that action at this time.
0 commit comments