I'm attempting to have 2 separate UITableViews inside of 1 UIViewController, but the trick is that I'm trying to do it so they both load from CoreDate.
I was trying to accomplish it by using the same style I would with UITableViewController, but I seem to be running into issues with that.
The way I'm getting my data for the tables is using a FRC (NSFetchedResultsController) but it would seem I'm only aloud to have 1 of those per UIViewController.
Here are my 2 FRC fetchers I'm using.
FRC 1:
func getFRCIngredients() -> NSFetchedResultsController<Ent_Ingredients> {
let fetchReq: NSFetchRequest<Ent_Ingredients> = Ent_Ingredients.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: "order", ascending: true)
let predicate = NSPredicate(format: "recipeRel == %@", self.recipe!)
fetchReq.sortDescriptors = [sortDescriptor]
fetchReq.predicate = predicate
let frc = NSFetchedResultsController(fetchRequest: fetchReq, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
return frc
}
FRC 2:
func getFRCDirections() -> NSFetchedResultsController<Ent_Directions> {
let fetchReq: NSFetchRequest<Ent_Directions> = Ent_Directions.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: "order", ascending: true)
let predicate = NSPredicate(format: "recipeRel == %@", self.recipe!)
fetchReq.sortDescriptors = [sortDescriptor]
fetchReq.predicate = predicate
let frc = NSFetchedResultsController(fetchRequest: fetchReq, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
return frc
}
I get the error inside the viewDidLoad()
after I try to set the delegate on the 2nd FRC to be self. Which makes sense when I think about it, you probably can't have it managing two separate sets of data.
Error: "libc++abi.dylib: terminating with uncaught exception of type NSException
"
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
tableViewIngredients.dataSource = self
tableViewIngredients.delegate = self
tableViewIngredients.register(UITableViewCell.self, forCellReuseIdentifier: "recipeIngredientCell")
tableViewDirections.dataSource = self
tableViewDirections.delegate = self
tableViewDirections.register(UITableViewCell.self, forCellReuseIdentifier: "recipeDirectionCell")
recipeName.text = recipe!.name
recipeImage.image = UIImage(data: recipe!.image as! Data)
oldName = recipe!.name!
frcIngredients = getFRCIngredients()
frcIngredients!.delegate = self
do {
try frcIngredients!.performFetch()
} catch {
fatalError("Failed to perform initial FRC fetch for Ingredients")
}
frcDirections = getFRCDirections()
frcDirections!.delegate = self //<- ***ERRORS HERE***
do {
try frcDirections!.performFetch()
} catch {
fatalError("Failed to perform initial FRC fetch for Ingredients")
}
NotificationCenter.default.addObserver(self, selector: #selector(AddRecipesVC.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
How can I accomplish what I'm trying to do here? I'm still relatively new to Xcode and Swift so any help would be very much appreciated.
Thank you!