NSPersistentStoreCoordinatorStoresDidChangeNotification with NSFetchedResultsController

Hi,


I'm using CoreData with iCloud and an NSFetchedResultsController for data preperation for the UITableView.


I have the following problem.

During initial App launch core data starts with the fallback store and the changes to the iCloud store. When this is completed the NSPersistentStoreCoordinatorStoresDidChangeNotification fires.

My problem now is that my NSFetchedResultsController is still reciving results from the old fallback store. Why is that?

The only solution I have found until now is to re-init the NSFetchedResultsController with the new persisitenStoreCoordinator.


Is there a better solution then this, because i want to use the coordinator with a lazy var like

lazy var fetchedResultsController: NSFetchedResultsController = {

let fetchRequest = NSFetchRequest(entityName: "Note")

let primarySortDescriptor = NSSortDescriptor(key: "creationDate", ascending: true)

fetchRequest.sortDescriptors = [primarySortDescriptor]

fetchRequest.predicate = NSPredicate(format: "notebook = %@", self.selectetNotebook)

let frc = NSFetchedResultsController(

fetchRequest: fetchRequest,

managedObjectContext: self.coreDataManager.managedObjectContext,

sectionNameKeyPath: nil,

cacheName: nil)

frc.delegate = self

return frc

}()


As far as i know there is no way to re-initialize a lazy var. Is that correct?


Any help and suggestions are welcome

Replies

Lazy is for one-time initialization only. Since upon NSPersistentStoreCoordinatorStoresDidChangeNotification you need to re-initialize the NSFetchedResultsController you can't use lazy and must use something else.