 import UIKit
 import CoreData
 
-class RestaurantTableViewController: UITableViewController {
+class RestaurantTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
 
     var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl",
         "Petite Oyster", "For Kee Restaurant", "Po's Atelier",

    var restaurantIsVisited = [Bool](count: 21, repeatedValue: false)
    
    var restaurants: [Restaurant] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.hidesBarsOnSwipe = true
        
        // core data code
        // create a NSFetchRequest for restaurant entity
        let fetchRequest = NSFetchRequest(entityName: "Restaurant")
        // specify how the results want to be sorted
        let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptor]
        
        if let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext {
            // initiliaze NSFetchResultsController
            fetchResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
            // and specify its delegate for monitoring data changes
            fetchResultsController.delegate = self
            
            var e: NSError?
            // execute the fetch request
            var result = fetchResultsController.performFetch(&e)
            // get the restaurant objects, cast them as array of Restaurant
            restaurants = fetchResultsController.fetchedObjects as [Restaurant]
            
            if e != nil {
                println("Error while fetching: \(e?.localizedDescription)")
            }
        }
    }
