{"action":"created","issue":{"url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1109","labels_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1109/labels{/name}","comments_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1109/comments","events_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1109/events","html_url":"https://github.com/magicalpanda/MagicalRecord/issues/1109","id":111665042,"number":1109,"title":"NSFetchedResultsController in saveWithBlockAndWait not calling delegate","user":{"login":"ProjectDent","id":1865635,"avatar_url":"https://avatars.githubusercontent.com/u/1865635?v=3","gravatar_id":"","url":"https://api.github.com/users/ProjectDent","html_url":"https://github.com/ProjectDent","followers_url":"https://api.github.com/users/ProjectDent/followers","following_url":"https://api.github.com/users/ProjectDent/following{/other_user}","gists_url":"https://api.github.com/users/ProjectDent/gists{/gist_id}","starred_url":"https://api.github.com/users/ProjectDent/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ProjectDent/subscriptions","organizations_url":"https://api.github.com/users/ProjectDent/orgs","repos_url":"https://api.github.com/users/ProjectDent/repos","events_url":"https://api.github.com/users/ProjectDent/events{/privacy}","received_events_url":"https://api.github.com/users/ProjectDent/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-15T17:16:24Z","updated_at":"2015-10-15T17:38:26Z","closed_at":null,"body":"This code compiles, and should work:\r\n\r\n\r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        MagicalRecord.saveWithBlockAndWait {\r\n            (context: NSManagedObjectContext!) -> Void in\r\n            let fetchRequest = Conversation.MR_requestAllSortedBy(\"mostRecentMessage.eventDate\", ascending: false)\r\n            self.fetchedResultsController = NSFetchedResultsController(\r\n                fetchRequest: fetchRequest,\r\n                managedObjectContext: context,\r\n                sectionNameKeyPath: nil,\r\n                cacheName: MessagesViewController.fetchedResultsCacheName())\r\n            self.fetchedResultsController.delegate = self\r\n            \r\n            NSFetchedResultsController.deleteCacheWithName(MessagesViewController.fetchedResultsCacheName())\r\n            self.fetchedResultsController.MR_performFetch()\r\n        }\r\n    }\r\n\r\nI have my delegate functions declared with `@objc`. But they just don't get called.\r\n\r\nIf I let this live outside the `saveWithBlock` function, using `NSManagedObjectContext.MR_context()` or `NSManagedObjectContext.MR_mainThreadContext()`, it crashes with this error:\r\n\r\n> '+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'Conversation''\r\n\r\nIf i use `MagicalRecordStack.defaultStack()!.context` as the context, as I discover it does internally in other functions when you don't specify a context, it doesn't crash. But this seems very dangerous, as it relies on this always being called on the main thread.\r\n\r\nHow am I supposed to implement NSFetchedResultsController?"},"comment":{"url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/comments/148468359","html_url":"https://github.com/magicalpanda/MagicalRecord/issues/1109#issuecomment-148468359","issue_url":"https://api.github.com/repos/magicalpanda/MagicalRecord/issues/1109","id":148468359,"user":{"login":"ProjectDent","id":1865635,"avatar_url":"https://avatars.githubusercontent.com/u/1865635?v=3","gravatar_id":"","url":"https://api.github.com/users/ProjectDent","html_url":"https://github.com/ProjectDent","followers_url":"https://api.github.com/users/ProjectDent/followers","following_url":"https://api.github.com/users/ProjectDent/following{/other_user}","gists_url":"https://api.github.com/users/ProjectDent/gists{/gist_id}","starred_url":"https://api.github.com/users/ProjectDent/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ProjectDent/subscriptions","organizations_url":"https://api.github.com/users/ProjectDent/orgs","repos_url":"https://api.github.com/users/ProjectDent/repos","events_url":"https://api.github.com/users/ProjectDent/events{/privacy}","received_events_url":"https://api.github.com/users/ProjectDent/received_events","type":"User","site_admin":false},"created_at":"2015-10-15T17:38:26Z","updated_at":"2015-10-15T17:38:26Z","body":"This also does not call the delegate:\r\n\r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n\r\n        MagicalRecord.saveWithBlockAndWait {\r\n            (context: NSManagedObjectContext!) -> Void in\r\n            \r\n            let fetchRequest = Conversation.MR_requestAllSortedBy(\"mostRecentMessage.eventDate\", ascending: false)\r\n            \r\n            self.fetchedResultsController = NSManagedObject.MR_fetchController(\r\n                fetchRequest,\r\n                delegate: self,\r\n                useFileCache: false,\r\n                groupedBy: \"eventDate\",\r\n                inContext: context)\r\n            \r\n            NSFetchedResultsController.deleteCacheWithName(MessagesViewController.fetchedResultsCacheName())\r\n            self.fetchedResultsController.MR_performFetch()\r\n\r\n        }\r\n    }"}}