Aunque los elementos se están guardando en los datos centrales, no pueden verlos hasta que se reinicie

Aquí hay un ejemplo en el que FetchedResultsController se utiliza en ViewController , y las salvaguardas de otro ViewController no se transmiten a él.

  1. MainVC.swift

     lazy var coreDataManager = CoreDataManager() // Initialize fetched request private var fetchRecordRequestController: NSFetchedResultsController = NSFetchedResultsController() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. // Initialize managedObjectContext let mainManagedObjectContext = coreDataManager.managedObjectContext // Create Fetch Request let fetchRequest = NSFetchRequest(entityName: "Record") // Configure Fetch Request fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)] // Initialize Fetched Results Controller fetchRecordRequestController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: mainManagedObjectContext, sectionNameKeyPath: nil, cacheName: nil) // Configure Fetched Results Controller fetchRecordRequestController.delegate = self // Fetch data from object do { try fetchRecordRequestController.performFetch() } catch let fetchError as NSError { print("(RootViewController)fetchError: \(fetchError), \(fetchError.userInfo)") } // Add observer in NSNotificationCenter for NSManagedObjectContextObjectsDidChange NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(managedObjectContextObjectsDidChange), name: NSManagedObjectContextObjectsDidChangeNotification, object: mainManagedObjectContext) override func viewWillDisappear(animated: Bool) { NSNotificationCenter.defaultCenter().removeObserver(self) } // MARK: - NSFetchedResultsController Delegate Methods func controllerWillChangeContent(controller: NSFetchedResultsController) { tableView.beginUpdates() } // Delete corresponding row in tableView func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { switch type { case .Insert: tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade) case .Delete: tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) case .Update: tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) case .Move: tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade) tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade) } } func controllerDidChangeContent(controller: NSFetchedResultsController) { tableView.endUpdates() } func managedObjectContextObjectsDidChange(notification: NSNotification) { let mainManagedObjectContext = coreDataManager.managedObjectContext guard let userInfo = notification.userInfo else { return } if let inserts = userInfo[NSInsertedObjectsKey] as? Set<NSManagedObject> where inserts.count > 0 { print("--- INSERTS ---") print(inserts) print("+++++++++++++++") do { try mainManagedObjectContext.save() } catch let saveError as NSError { print("(RootViewController)saveError: \(saveError), \(saveError.userInfo)") } } if let updates = userInfo[NSUpdatedObjectsKey] as? Set<NSManagedObject> where updates.count > 0 { print("--- UPDATES ---") for update in updates { print(update.changedValues()) } print("+++++++++++++++") } if let deletes = userInfo[NSDeletedObjectsKey] as? Set<NSManagedObject> where deletes.count > 0 { print("--- DELETES ---") print(deletes) print("+++++++++++++++") } } 
  2. AddRecordVC.swift

     // Method to save the extracted values to CoreData Objects private func saveValues() { // Initializing let mainManagedObjectContext = coreDataManager.managedObjectContext // Initialize entity description let RecordDescription = NSEntityDescription.entityForName("Record",inManagedObjectContext:mainManagedObjectContext) let record = Record(entity: RecordDescription!, insertIntoManagedObjectContext: mainManagedObjectContext) // Set object values from temporary variables record.date = dateTemp! record.tareWeight = tareWeightTemp record.grossWeight = grossWeightTemp record.nettWeight = nettWeightTemp record.numberOfBags = numberOfBagsTemp record.weightOfBags = weightOfBagsTemp record.rivaju = rivajuTemp record.putti = puttiTemp record.rateOfPaddy = rateOfPaddyTemp record.amount = amountTemp // Save the object do { if mainManagedObjectContext.hasChanges { try mainManagedObjectContext.save() } } catch let saveError as NSError { print("(AddRecordViewController)saveError: \(saveError), \(saveError.userInfo)") } } 

Una vez, AddRuleVC regresa a RootVC, la vista no se actualiza. Pero después de que la aplicación se abandona y se relanza, muestra los datos. Se utiliza una class separada para CoreDataStack, que es como se muestra a continuación.

  1. CoreDataManager.swift

     class CoreDataManager: NSObject { // MARK: - Core Data Stack lazy var managedObjectModel: NSManagedObjectModel = { let modelURL = NSBundle.mainBundle().URLForResource("App Name", withExtension: "momd")! return NSManagedObjectModel(contentsOfURL: modelURL)! }() lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { // Create the coordinator and store let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite") var failureReason = "There was an error creating or loading the application's saved data." do { try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) } catch { // Report any error we got. var dict = [String: AnyObject]() dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" dict[NSLocalizedFailureReasonErrorKey] = failureReason dict[NSUnderlyingErrorKey] = error as NSError let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") abort() } return coordinator }() lazy var managedObjectContext: NSManagedObjectContext = { let coordinator = self.persistentStoreCoordinator var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) managedObjectContext.persistentStoreCoordinator = coordinator managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy return managedObjectContext }() 

    // MARK: – Métodos auxiliares

     func saveContext () { if managedObjectContext.hasChanges { do { try managedObjectContext.save() } catch { let nserror = error as NSError NSLog("Unresolved error \(nserror), \(nserror.userInfo)") abort() } } } lazy var applicationDocumentsDirectory: NSURL = { let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) return urls[urls.count-1] }() } 

Su class CoreDataManager está generando un nuevo context de object gestionado dentro de AddRecordVC diferente al de MainVC modo que cuando guarde el segundo context, la tienda persistente se actualizará pero los cambios no se reflejarán en el primer context a less que combine los cambios.

Esto no es bueno, solo quiere un context para el hilo principal. Entonces, lo que debe hacer es usar la dependency injection para el context del object administrado. CoreDataManager el CoreDataManager cuando su aplicación se inicie y luego inyecte el context en MainVC , luego cuando AddRecordVC simplemente pase el mismo context a lo largo también allí.