     }
 
     @objc private func mergeChangedFromMainQueueContextSaveNotification(notification: NSNotification) {
-        for context in backgroundContextsNeedingRefresh {
-            context.performBlockAndWait() {
-                context.mergeChangesFromContextDidSaveNotification(notification)
+        dispatch_sync(dispatch_queue_create("com.bignerdranch.coredatastack.locking.queue", nil)) { [unowned self] in
+            if let contexts = self.backgroundContextsNeedingRefresh.allObjects as? [NSManagedObjectContext] {
+                for context in contexts {
+                    context.performBlockAndWait() {
+                        context.mergeChangesFromContextDidSaveNotification(notification)
+                    }
+                }
             }
         }
     }


        // Optionally refresh this worker moc whenever the main MOC saves.
        if shouldReceiveUpdates {
            backgroundContextsNeedingRefresh.append(context)
        }

        return context

*/

public class ThreadConfinementStack: CoreDataStack {
    private var backgroundContextsNeedingRefresh = NSHashTable.weakObjectsHashTable()

    /**
    Primary managed object context for main queue work.
