Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.

Commit b0fe12f

Browse files
Kmohamed3lvis
authored andcommittedJan 31, 2018
Fix listen to specific NSManagedObjectContext (#112)
* Fix listen to specific NSManagedObjectContext Adding name to Background context and check in notification. * Fix typo
1 parent 9f53bac commit b0fe12f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed
 

‎Source/DATAStack.swift

+8
Original file line numberDiff line numberDiff 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 {

0 commit comments

Comments
 (0)
This repository has been archived.