     }
     
     private func addObservers() {
-        NSNotificationCenter.defaultCenter().addObserver(
-            self,
-            selector: Selector("persistentStoreCoordinatorStoresWillChange:"),
-            name: NSPersistentStoreCoordinatorStoresWillChangeNotification,
-            object: self.coordinator
-        )
-        
-        NSNotificationCenter.defaultCenter().addObserver(
-            self,
-            selector: Selector("persistentStoreCoordinatorStoresDidChange:"),
-            name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
-            object: self.coordinator
-        )
-        
-        NSNotificationCenter.defaultCenter().addObserver(
-            self,
-            selector: Selector("persistentStoreDidImportUbiquitousContentChanges:"),
-            name: NSPersistentStoreDidImportUbiquitousContentChangesNotification,
-            object: self.coordinator
-        )
+        if self.contextOptions.stackType == .SQLite && self.contextOptions.ubiquityEnabled {
+            NSNotificationCenter.defaultCenter().addObserver(
+                self,
+                selector: Selector("persistentStoreCoordinatorStoresWillChange:"),
+                name: NSPersistentStoreCoordinatorStoresWillChangeNotification,
+                object: self.coordinator
+            )
+            
+            NSNotificationCenter.defaultCenter().addObserver(
+                self,
+                selector: Selector("persistentStoreCoordinatorStoresDidChange:"),
+                name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
+                object: self.coordinator
+            )
+            
+            NSNotificationCenter.defaultCenter().addObserver(
+                self,
+                selector: Selector("persistentStoreDidImportUbiquitousContentChanges:"),
+                name: NSPersistentStoreDidImportUbiquitousContentChangesNotification,
+                object: self.coordinator
+            )
+        }
     }
     
     private func removeObservers() {

    case InMemory
}

private var backgroundContexts = NSMutableArray()

internal final class Stack {
  
    private let contextOptions: ContextOptions


private final class StackBackgroundManagedObjectContext: NSManagedObjectContext {
    
    private let stack: Stack
    
    private init(stack: Stack) {
        self.stack = stack
        super.init(concurrencyType: .PrivateQueueConcurrencyType)
        
        self.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        self.undoManager = nil
        self.parentContext = self.stack.rootManagedObjectContext
        

        self.addObservers()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    deinit {
        self.removeObservers()
    }
    
    private func addObservers() {
        if self.stack.contextOptions.stackType == .SQLite && self.stack.contextOptions.ubiquityEnabled {
            NSNotificationCenter.defaultCenter().addObserver(
                self,
                selector: Selector("persistentStoreCoordinatorStoresWillChange:"),
                name: NSPersistentStoreCoordinatorStoresWillChangeNotification,
                object: self.stack.coordinator
            )
            
            NSNotificationCenter.defaultCenter().addObserver(
                self,
                selector: Selector("persistentStoreCoordinatorStoresDidChange:"),
                name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
                object: self.stack.coordinator
            )
            
            NSNotificationCenter.defaultCenter().addObserver(
                self,
                selector: Selector("persistentStoreDidImportUbiquitousContentChanges:"),
                name: NSPersistentStoreDidImportUbiquitousContentChangesNotification,
                object: self.stack.coordinator
            )
        }
        
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: Selector("backgroundManagedObjectContextDidSave:"),
            name: NSManagedObjectContextDidSaveNotification,
            object: self
        )
    }
    
    private func removeObservers() {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextDidSaveNotification, object: self)

        if self.stack.contextOptions.stackType == .SQLite && self.stack.contextOptions.ubiquityEnabled {
            NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: self.stack.coordinator)
            NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: self.stack.coordinator)
            NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: self.stack.coordinator)
        }
        
    }

    @objc private func backgroundManagedObjectContextDidSave(notification: NSNotification) {
        if let mainContext = self.stack.mainManagedObjectContext {
            mainContext.performBlock {
                mainContext.mergeChangesFromContextDidSaveNotification(notification)
            }
        }
    }
    
    @objc private func persistentStoreCoordinatorStoresWillChange(notification: NSNotification) {
        self.performBlock {
            var error: NSError? = nil
            self.save(&error)
            self.reset()
        }
    }
    
    @objc private func persistentStoreCoordinatorStoresDidChange(notification: NSNotification) {
        self.performBlock {
            self.reset()
        }
    }
    
    @objc private func persistentStoreDidImportUbiquitousContentChanges(notification: NSNotification) {
        self.performBlock {
            self.mergeChangesFromContextDidSaveNotification(notification)
        }
    }
}

