 
 private final class StackBackgroundManagedObjectContext: NSManagedObjectContext {
     
-    private var mainContext: NSManagedObjectContext! {
-        didSet {
-            if self.mainContext != oldValue {
-                if self.mainContext != nil {
-                    backgroundContexts.addObject(self)
-                    
-                    NSNotificationCenter.defaultCenter().addObserver(
-                        self,
-                        selector: Selector("backgroundManagedObjectContextDidSave:"),
-                        name: NSManagedObjectContextDidSaveNotification,
-                        object: self
-                    )
-                }
-                else {
-                    NSNotificationCenter.defaultCenter().removeObserver(self, name: NSManagedObjectContextDidSaveNotification, object: self)
-                    
-                    backgroundContexts.removeObject(self)
-                }
-            }
-        }
-    }
-    
+    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.mainContext = nil // setting mainContext to nil also removes notifications
-    }
-    
+        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) {
-        self.mainContext.performBlock {
-            self.mainContext.mergeChangesFromContextDidSaveNotification(notification)
-        }
-    }
-    
-}
-
+        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)
+        }
+    }
+}
+

            
            currentContext = currentContext?.parentContext
        }
        
        for backgroundContext in backgroundContexts {
            if let c = backgroundContext as? StackBackgroundManagedObjectContext {
                c.performBlock {
                    var error: NSError? = nil
                    c.save(&error)
                    c.reset()
                }
            }
        }
    }
    
    @objc private func persistentStoreCoordinatorStoresDidChange(notification: NSNotification) {

            }
            
            currentContext = currentContext?.parentContext
        }
    }
    
