【发布时间】:2015-08-04 15:12:40
【问题描述】:
我有一个表格视图,我在其中创建了一组(部分)和伙伴(行)的分段列表。 buddies和bundles之间的关系在核心数据中是多对多的。我想使用 NSFetchedResultsController 为这个 tableview 提供数据。 注意:我使用this tutorial 来设置 NSFetchedResultsController
这是我的 NSFetchedResultsController:
lazy var fetchedResultsController: NSFetchedResultsController = {
let bunchesFetchRequest = NSFetchRequest(entityName: Constants.CoreData.bunch)
// "name" refers to the bunch name
let primarySortDescriptor = NSSortDescriptor(key: "name", ascending: true, selector: "caseInsensitiveCompare:")
bunchesFetchRequest.sortDescriptors = [primarySortDescriptor]
let frc = NSFetchedResultsController(
fetchRequest: bunchesFetchRequest,
managedObjectContext: CoreDataStackManager.sharedInstance().managedObjectContext!,
sectionNameKeyPath: "name",
cacheName: nil
)
frc.delegate = self
return frc
}()
当我去获取一堆好友的数量时,我总是得到一个没有好友的集合。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController.sections {
let currentSection = sections[section] as! NSFetchedResultsSectionInfo
let bunch = currentSection.objects[0] as! Bunch
return bunch.buddies.count // always returns 0
}
return 0
}
与 NSFetchedResultsController 或其他我应该得到计数的方式有什么额外的关系吗?如果您可以用核心数据内部结构解释这里发生了什么,那也会很有帮助。谢谢!
【问题讨论】:
标签: ios swift core-data many-to-many nsfetchedresultscontroller