coredata

-How the table displays changes
-How a user can add a new note
-Saving and persisting changes
-Memory management

class CoreDataTableViewController: UITableViewController {
	
	var fetchedResultsController : NSFetchedResultsController<NSFetchRequestsResult>?{
		didSet {
			fetchedResultsController?.delegate = self
			executeSearch()
			tableView.reloadData()
		}
	}

	init(fetchedResultsController fc : NSFetchedResultsController<NSFetchRequestResult>, style: UITableViewStyle = .plain){
		fetchedResultsController = fc
		super.init(style: style)
	}

	required init?(coder aDecoder: NSCoder){
		super.init(coder: aDecoder)
	}
}
class NotebooksViewController: CoreDataTableViewController {
	
	// MARK: Life Cycle

	overeride func viewDidLoad(){
		super.viewDidLoad()

		title = "CoolNotes"

		let delegate = UIApplication.shared.delegate as! AppDelegate
		let stack = delegate.stack

		let fr = NSFetchRequest<NSFetchRequestResult>(entityName: "Notebook")
		fr.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true), NSSortDescriptor(key: "creationDate", ascending: false)]

		fetchedResultsController = NSFetchedResultsController(fetchRequest:  fr, managedObjectContext: stack.context, sectionNameKeyPath: nil, cacheName: nil)

	}

	override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let nb = fetchedResultsController!.object(at: indexPath) as! Notebook

		let cell = tableView.dequeueReusableCell(withIdentifier: "NotebookCell", for: indexPath)

		cell.textLabel?.text = nb.name
		cell.detailTextLabel?.text = "\(nb.notes!.count) notes"

		return cell
	}
}