0
votes

I have a very simple UITableView that loads core data records using a NSFetchedResultsController. I have re-written the Objective-C code to Swift. I seem to have an issue when running the Swift code on an iOS 7 simulator but ok on iOS 8. As far as I know Swift is supposed to be backward compatible for iOS 7.

The error I get when running iOS 7 but not 8 is:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSFetchRequest could not locate an NSEntityDescription for entity name 'Category''

For some reason when executing the NSFetchRequest it doesn't like my core data entity Category on iOS 7 but fine on iOS 8, any ideas?

Relevant code:

class CategoriesListTableViewController : CoreDataTableViewController, AddEditCategoryTableViewControllerDelegate {
    
    let kCategoryEntityID = "Category"
    let kCategoryCellID = "Category Cell"
    let kCategoryEntityParentAttributeID = "parent"
    let kCategoryEntityNameAttributeID = "name"
    let kCategoriesCacheID = "Categories"
    let kAddCategorySegue = "Add Category"
    let kEditCategorySegue = "Edit Category"
    
    var moc = NSManagedObjectContext()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        let app = UIApplication.sharedApplication().delegate as AppDelegate
        moc = app.cdh().context
        
        //debugcode
        let mom = moc.persistentStoreCoordinator.managedObjectModel
        let entities = mom.entitiesByName
        let entityNames = entities.description
        println("All loaded entities are: \(entityNames)")
        
        self.setupFetchedResultsController()
    }
    
    func setupFetchedResultsController() // attaches an NSFetchRequest to this UITableViewController
    {
        
        
        let request = NSFetchRequest(entityName: kCategoryEntityID)
        let sortParent = NSSortDescriptor(key: kCategoryEntityParentAttributeID, ascending: true)
        let sortName = NSSortDescriptor(key: kCategoryEntityNameAttributeID, ascending: true)
        let sortDescriptors = [sortParent, sortName]
        
        request.sortDescriptors = sortDescriptors
        
        self.fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: kCategoryEntityParentAttributeID, cacheName: kCategoriesCacheID)
    }
1

1 Answers

0
votes

It seems that you might not be creating your managed object context correctly. The fetch request created with an entity name string is not "complete" until it becomes associated with the actual managed object context. The managed object context in turn associates it with the managed object model which contains the necessary entity descriptions.

In this case this should happen during the initialization of the fetched results controller. Check if the correct managed object context is loaded as expected when you create the FRC.

As your different results appear in iOS7 and 8 respectively, consider deleting the app completely from the simulator first. Also, try to clean the project before building again.