Chapter 2 - insertSampleData() - missing save?

Hey guys, I’m going through the chapter 2 material and there was an earlier example that inserted a new object into the ManagedObjectContext and later called a save on the MangedObjectContext. However, in the following function, it creates an entity, but never calls a save on the MangedObjectContext. Am I missing something? I thought objects are saved to the data source only once an explicit save is called.

func insertSampleData() {
    let fetchRequest = NSFetchRequest(entityName: "Bowtie")
    fetchRequest.predicate = NSPredicate(format: "searchKey != nil")
    let count = managedContext.countForFetchRequest(fetchRequest, error: nil)
    if count > 0 {return }
    let path = NSBundle.mainBundle().pathForResource("SampleData", ofType: "plist")
    let dataArray = NSArray(contentsOfFile: path!)!
    for dict : AnyObject in dataArray {
      let entity = NSEntityDescription.entityForName("Bowtie", inManagedObjectContext: managedContext)
      let bowtie = Bowtie(entity: entity!, insertIntoManagedObjectContext: managedContext)
      let btDict = dict as! NSDictionary
    = btDict["name"] as? String
      bowtie.searchKey = btDict["searchKey"] as? String
      bowtie.rating = btDict["rating"] as? NSNumber
      let tintColorDict = btDict["tintColor"] as? NSDictionary
      bowtie.tintColor = colorFromDict(tintColorDict!)
      let imageName = btDict["imageName"] as? String
      let image = UIImage(named:imageName!)
      let photoData = UIImagePNGRepresentation(image!)
      bowtie.photoData = photoData
      bowtie.lastWorn = btDict["lastWorn"] as? NSDate
      bowtie.timesWorn = btDict["timesWorn"] as? NSNumber
      bowtie.isFavorite = btDict["isFavorite"] as? NSNumber

Code that calls save:

// Save test bow tie
let bowtie = NSEntityDescription .insertNewObjectForEntityForName(“Bowtie”,
inManagedObjectContext: managedObjectContext) as! Bowtie = “My bow tie”
bowtie.lastWorn = NSDate()
do {
} catch let error as NSError {
print(“Saving error: (error.localizedDescription)”)

Yes there should be a being called. Perhaps it was an omission when they updated the code because it was there in v1.2. Let me get back to you on that.

Ok, if you run the app and check for the sqlite file, it indeed doesnt get created. So those bowties just got pulled from the plist but they didnt get added as entities. I guess the idea was not to waste space creating a database if no bowtie was actually used. If you rate a bowtie you can see the database gets a record added to it with all other bowties.