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!
So it turns out I had made a silly error. I forgot that I had not updated the attributes names in the Ent_Directions
Entity, so I was always calling an attribute that didn't exist. But Just in case this helps someone later on, this is the code I ended up using to get the 2 UITableViews working in the single UIViewController. I removed the extra code that didn't pertain to the UITableViews.
Swift 3.0:
class EditRecipesVC: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
// MARK: - Constants and Variables
let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var frcIngredients: NSFetchedResultsController<Ent_Ingredients>?
var frcDirections: NSFetchedResultsController<Ent_Directions>?
var recipe: Ent_Recipes?
// MARK: - Class Loading Functions
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
tableViewIngredients.dataSource = self
tableViewIngredients.delegate = self
tableViewIngredients.rowHeight = UITableViewAutomaticDimension
tableViewIngredients.estimatedRowHeight = 40
tableViewDirections.dataSource = self
tableViewDirections.delegate = self
tableViewDirections.rowHeight = UITableViewAutomaticDimension
tableViewDirections.estimatedRowHeight = 40
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
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)
}
// MARK: - Outlets and Actions
@IBOutlet var tableViewIngredients: UITableView!
@IBOutlet var tableViewDirections: UITableView!
// MARK: - Table View Data Source
func numberOfSections(in tableView: UITableView) -> Int {
var frc: NSFetchedResultsController<NSFetchRequestResult>
if (tableView == tableViewIngredients) {
frc = frcIngredients as! NSFetchedResultsController<NSFetchRequestResult>
} else {
frc = frcDirections as! NSFetchedResultsController<NSFetchRequestResult>
}
if let sections = frc.sections {
return sections.count
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var frc: NSFetchedResultsController<NSFetchRequestResult>
if (tableView == tableViewIngredients) {
frc = frcIngredients as! NSFetchedResultsController<NSFetchRequestResult>
} else {
frc = frcDirections as! NSFetchedResultsController<NSFetchRequestResult>
}
if let sections = frc.sections {
let currentSection = sections[section]
return currentSection.numberOfObjects
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (tableView == tableViewIngredients) {
let cell: RecipeIngredientTVCell = tableView.dequeueReusableCell(withIdentifier: "recipeIngredientCell", for: indexPath) as! RecipeIngredientTVCell
return cell
} else {
let cell: RecipeDirectionTVCell = tableView.dequeueReusableCell(withIdentifier: "recipeDirectionCell", for: indexPath) as! RecipeDirectionTVCell
return cell
}
}
// MARK: - Custom Functions
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
}
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
}
}