我对Swift和Core Data很陌生,但我认为我掌握了基础知识。但是,我有一个简单的核心数据模型的问题,我想抓住与多对多关系中的另一个实体相关的所有实体。我想要做的是在表视图中选择一个父对象,并在另一个视图中显示其子元素。
所以我使用以下代码从ParentsVC传递父对象:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let CHILDREN_TAB_INDEX = 1
var navigationController = self.tabBarController?.viewControllers?[CHILDREN_TAB_INDEX] as UINavigationController
if navigationController.topViewController is ChildrenVC {
var childrenVC = navigationController.topViewController as ChildrenVC
let parent = self.fetchedResultsController.objectAtIndexPath(self.menuTableView.indexPathForSelectedRow()!) as Parent
childrenVC.currentParent = parent
self.tabBarController?.selectedIndex = CHILDREN_TAB_INDEX
}
}
然后在ChildrenVC中,我执行以下提取以获取子对象:
var _fetchedResultsController: NSFetchedResultsController? = nil
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let moc:NSManagedObjectContext = coreDataHelper.backgroundContext!
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("Child", inManagedObjectContext: moc)
fetchRequest.entity = entity
fetchRequest.fetchBatchSize = 20
let sortDescriptor = NSSortDescriptor(key: "name", ascending: false)
let predicate = NSPredicate(format: "parent == %@", self.currentParent)
fetchRequest.predicate = predicate
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
abort()
}
_fetchedResultsController?.delegate = self
return _fetchedResultsController!
}
添加新子代码的代码(在ParentsVC中完成):
func addNewParent(parentName:String) {
let moc:NSManagedObjectContext = coreDataHelper.backgroundContext!
var parent:Parent = NSEntityDescription.insertNewObjectForEntityForName("Parent", inManagedObjectContext: moc) as Parent
parent.name = parentName
for n in 1...5 {
var child:Child = NSEntityDescription.insertNewObjectForEntityForName("Child", inManagedObjectContext: moc) as Child
child.name = "A"
child.parent = parent
}
coreDataHelper.saveContext(moc)
menuTableView.reloadData()
}
不是根据我点击的父级列出不同的子元素,而是根据我选择的父级获得相同的子项(子项在ChildrenVC中可编辑,因此我可以区分)。但是当我重新启动/重建应用程序时,我会随机获得另一组为所有父母显示的子项。我在这里缺少什么?
答案 0 :(得分:1)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let CHILDREN_TAB_INDEX = 1
var navigationController = self.tabBarController?.viewControllers?[CHILDREN_TAB_INDEX] as UINavigationController
if navigationController.topViewController is ChildrenVC {
var childrenVC = navigationController.topViewController as ChildrenVC
let parent = self.fetchedResultsController.objectAtIndexPath(self.menuTableView.indexPathForSelectedRow()!) as Parent
childrenVC.currentParent = parent
childrenVC._fetchedResultsController = nil
self.tabBarController?.selectedIndex = CHILDREN_TAB_INDEX
}
}