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)
}