ios - One object in multiple sections when sorting with NSFetchedResultsController
问题描述
I’ve just started using Core Data and NSFetchedResultsController and I'm kind of stuck. I've a Book entity which has a category attribute of type String. The data is loaded into a collection view and grouped by sections based on this category. All’s working fine this way, but the problem is that a Book should be able to appear in multiple categories (and collection view sections). I could change the category’s type to an Array of String, but how would I do the sorting then?
let request = Book.fetchedRequest() as NSFetchRequest<Book>
let categorySort = NSSortDescriptor(key: #keyPath(Book.category)), ascending:true)
request.sortDescriptors = [categorySort]
do {
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: #keyPath(Book.category)), cacheName: nil)
try fetchedResultsController.performFetch()
} catch let error as NSError {
print(“Could not fetch. \(error), \(error.userInfo)”)
}
Should I just give up on using NSFetchedResultsController or is it possible to make this work?
解决方案
...一本书应该能够出现在多个类别(和收藏视图部分)中。
我应该放弃使用 NSFetchedResultsController 还是可以让它工作?
您描述的内容无法通过获取的结果控制器实现。
NSFetchedResultsController
获取所有获取的对象(可能由谓词过滤),根据排序描述符对它们进行排序,并根据sectionNameKeyPath
参数对它们进行分组。
获取的结果控制器不可能多次提供相同的对象(在相同或不同的部分)。
推荐阅读
- r - 如何从 case_when() 返回多个值或一个小标题?
- excel - VBA函数使用列名而不是单元格地址
- java - Spring Security 5 - 我的自定义 UserDetailsService 没有被调用
- autodesk-forge - 激活 MemoryLimited 时未加载属性数据库
- go - time.Now() 时间戳在末尾加 000
- javascript - JavaScript 数组中每天一个随机项目
- razor - 如何从 2sxc 内容字段输出页面 ID 而不是页面 URL?
- php - php联系表格不返回消息
- r - CCF 与 ccf:有什么区别?
- python - 概率矩阵 Numpy 的终端概率