I am quite new to Swift and Core Data, but I think I got the basics. However, I have a problem with a simple core data model where I want to grab all entities that relates to a another entity in a to-many relationship. What I want to do is select a parent object in a table view and display its child elements in another view.
So I am passing the parent object from ParentsVC using this code:
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
}
}
Then in ChildrenVC I do the following fetch to get the child objects:
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!
}
And the code to add new children (done in 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()
}
Instead of listing different child elements depending on which parent I tap, I just get the same children whichever parent I pick (the children are editable in ChildrenVC so I can tell the difference). But when I restart/rebuild the app I randomly get another set of children that appears for all the parents. What am I missing here?