From 24eb402b0615e6df06e4966c42b0da18cb5abcf5 Mon Sep 17 00:00:00 2001 From: Kmohamed Date: Wed, 31 Jan 2018 18:22:09 +0200 Subject: [PATCH 1/2] Fix listen to specific NSManagedObjectContext Adding name to Background context and check in notification. --- Source/DATAStack.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Source/DATAStack.swift b/Source/DATAStack.swift index ad2271e..44b4050 100644 --- a/Source/DATAStack.swift +++ b/Source/DATAStack.swift @@ -26,6 +26,7 @@ import CoreData private var model: NSManagedObjectModel private var containerURL = URL.directoryURL() + private let backgroundContextName = "DATAStack.BackgrounContext" /** The context for the main queue. Please do not use this to mutate data, use `performInNewBackgroundContext` @@ -232,6 +233,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 @@ -373,6 +375,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 conetxt = notification.object as? NSManagedObjectContext + if conetxt?.name != backgroundContextName { + return + } + if Thread.isMainThread && TestCheck.isTesting == false { throw NSError(info: "Background context saved in the main thread. Use context's `performBlock`", previousError: nil) } else { From 0710a7d47009c7afa764854bf5e619e20878065d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= <3lvis@users.noreply.github.com> Date: Wed, 31 Jan 2018 21:55:04 +0100 Subject: [PATCH 2/2] Fix typo --- Source/DATAStack.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Source/DATAStack.swift b/Source/DATAStack.swift index 44b4050..0b0baa1 100644 --- a/Source/DATAStack.swift +++ b/Source/DATAStack.swift @@ -26,7 +26,8 @@ import CoreData private var model: NSManagedObjectModel private var containerURL = URL.directoryURL() - private let backgroundContextName = "DATAStack.BackgrounContext" + + private let backgroundContextName = "DATAStack.backgroundContextName" /** The context for the main queue. Please do not use this to mutate data, use `performInNewBackgroundContext` @@ -375,11 +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 conetxt = notification.object as? NSManagedObjectContext - if conetxt?.name != backgroundContextName { + 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 {