如果我使用一对一,我可以显示所有相关的NSSet记录,但很多对很多人似乎都没有。
在我的segue上,我将体操运动员传给了一个VC,我希望在其中显示所有相关的会议,这些会议在一对多的情况下都能正常工作。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let objs = fetchedResultsController.fetchedObjects where objs.count > 0 {
let item = objs[indexPath.row] as! Gymnast
performSegueWithIdentifier("displayMeets", sender: item)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "displayMeets" {
let vc = segue.destinationViewController as! SelectMeetVC
vc.selectedGymnast = sender as? Gymnast
}
}
除非我注释掉NSPREDICATE
,否则我会崩溃func loadFetchRequest() {
self.fetchedResultsController = nil
let fetchRequest = NSFetchRequest(entityName: "Meet")
let sortDescriptor1 = NSSortDescriptor(key: "meetName", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor1]
//let filter = NSPredicate(format: "gymnasts.fullName == %@", "\(selectedGymnast.fullName!)")
//fetchRequest.predicate = filter
let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: AD.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
controller.delegate = self
fetchedResultsController = controller
do {
try fetchedResultsController.performFetch()
} catch {
fatalError("Error retrieving core data")
}
}
答案 0 :(得分:0)
由于gymnasts
关系为to-many,因此您需要修改谓词,以便在其Meet
匹配的任何时选择gymnasts
:
let filter = NSPredicate(format: "ANY gymnasts.fullName == %@", "\(selectedGymnast.fullName!)")
另请注意,无需使用fullName
属性 - 您可以匹配Gymnast
对象本身:
let filter = NSPredicate(format: "ANY gymnasts == %@", selectedGymnast)