此代码位于我的Entity + CoreDataProperties.swift文件中:
@nonobjc public class func mFetchRequest() -> NSFetchRequest<Entity> {
return (getContext().persistentStoreCoordinator?.managedObjectModel.fetchRequestTemplate(forName: "MEntityRequest"))! as! NSFetchRequest<Entity>
}
在我的NSFetchedResultsControllerDelegate扩展代码中,我像这样调用这个函数:
var fetchedResultsController: NSFetchedResultsController<Entity> {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest: NSFetchRequest<Entity> = Entity.mFetchRequest()
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
do {
try _fetchedResultsController!.performFetch()
} catch let error as NSError{
print("Unresolved Fetch Error \(error), \(error.userInfo)")
}
return _fetchedResultsController!
}
通过在获取请求上设置sortDescriptors,我得到错误:&#39; NSInternalInconsistencyException&#39;,原因:&#39;无法修改不可变模型中的命名提取请求。&#39;
当我没有设置sortDescriptors:&#39; NSInvalidArgumentException&#39;时,原因:&#39; NSFetchedResultsController的实例需要带有排序描述符的获取请求&#39;
我做得不好? NSFetchResultsController可以不使用获取请求模板吗?