Ios NSFetchedResultsController';s节数组元素';s的名称更新不正确
我有一个NSFetchedResultsController的简单用法,它定义了类似于这个人为示例的东西Ios NSFetchedResultsController';s节数组元素';s的名称更新不正确,ios,swift,core-data,nsfetchedresultscontroller,Ios,Swift,Core Data,Nsfetchedresultscontroller,我有一个NSFetchedResultsController的简单用法,它定义了类似于这个人为示例的东西 let request: NSFetchRequest<StudentEntity> = StudentEntity.fetchRequest() request.sortDescriptors = [ NSSortDescriptor(key: "class.name", ascending: true, selector: #selector(NSString.l
let request: NSFetchRequest<StudentEntity> = StudentEntity.fetchRequest()
request.sortDescriptors = [
NSSortDescriptor(key: "class.name", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))
]
fetchedResultsController = NSFetchedResultsController<StudentEntity>(
fetchRequest: request,
managedObjectContext: context,
sectionNameKeyPath: "class.name",
cacheName: nil)
这将导致以下数据源方法正确运行
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let sectionInfo = fetchedResultsController!.sections![section]
let sectionTitle = sectionInfo.name
let sectionStudentEntity = sectionInfo.objects![0] as! StudentEntity
let sectionTitleFromStudent = sectionStudentEntity.class!.name!
但是,sectionTitle将不会更新并显示旧的类名,而sectionTitleFromStudent将正确显示更新后的新类名
我认为这是因为sectionNameKeyPath是一个嵌套路径,尽管类实体正在更新,但fetchedResultsController却无法检测到学生实体本身的任何更改,因此无法重新创建这些节名称。感觉像一个bug,但谢天谢地,我可以通过使用正确更新的sectionTitleFromStudent字符串来解决它
有人有什么想法,或者自己也遇到过同样的问题吗
CheersFetchedResultsController将仅观察其获取所依据的实体(在您的情况下是
Student
对象)的对象更改。因此,它不知道相关类
对象属性的更改。为了让它知道,您可以重做performFetch
,这将触发它更新其获取的对象和部分。(请注意,performFetch
绕过了正常的FRC更改跟踪,因此不会触发FRC委托方法,但这应该不是问题,因为您正在对tableView执行完整的重新加载数据
。FetchedResultsController将只观察对作为其获取基础的实体的对象所做的更改(在您的案例中是Student
对象)。因此,它不知道相关类
对象属性的更改。为了让它知道,您可以重做performFetch
,这将触发它更新其获取的对象和部分。(请注意,performFetch
绕过了正常的FRC更改跟踪,因此不会触发FRC委托方法,但这应该不是问题,因为您正在tableView上执行完整的重新加载数据
).在重新加载数据之前,您是否尝试过执行性能蚀刻
?我没有尝试过,尝试它确实解决了问题。我猜它无法检测到链接实体的依赖性仅更改。谢谢,如果您想将其作为一个答案,我会接受。干杯!我在更改时遇到了相同的问题属于sectionInfo!.numberOfObjects
(用作我的章节标题的一部分)NSFetchedResultsController将无法检测到并且无法正确更新tableView节标题。仍在寻找解决方案。在重新加载数据之前,您是否尝试过执行performFetch
?我没有尝试过,尝试它确实解决了问题。我猜它无法检测链接实体的依赖性仅更改。谢谢,如果您想将此作为答案,我将接受。干杯!我在更改sectionInfo!.numberOfObjects
(用作我的部分标题)时遇到了相同的问题NSFetchedResultsController将无法检测到并且无法正确更新tableView节标题。仍在寻找解决方案。
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let sectionInfo = fetchedResultsController!.sections![section]
let sectionTitle = sectionInfo.name
let sectionStudentEntity = sectionInfo.objects![0] as! StudentEntity
let sectionTitleFromStudent = sectionStudentEntity.class!.name!