Q:

Saving Multiple Data Entries into CoreData

I'm trying to populate my entity in CoreData for a new app.  I made a new function named initializeDB() which sets up new values for the entity and then calls the save() function (within a do try block of course).  However it turns out that only the last entity is actually saved. I'm curious as to know why this is the case.  I know that only the last one is saved since I do a fetch and only see that the last one is returned.
        date = myTimeZoneDateFormatter1.dateFromString("2016-02-03")!
        entity.setValue("3.1", forKey: "distanceOfRun")
        entity.setValue(date, forKey: "dateOfRun")
        do{
            try moc.save()
            print("save was successful")
        }
        catch{

            print("Entry was not saved")        }

        date = myTimeZoneDateFormatter1.dateFromString("2016-02-05")!
        entity.setValue("3.1", forKey: "distanceOfRun")
        entity.setValue(date, forKey: "dateOfRun")
        do{
            try moc.save()
            print("save was successful")
        }
        catch{

            print("Entry was not saved")        }

        date = myTimeZoneDateFormatter1.dateFromString("2016-02-07")!
        entity.setValue("3", forKey: "distanceOfRun")
        entity.setValue(date, forKey: "dateOfRun")
        do{
            try moc.save()
            print("save was successful")
        }
        catch{

            print("Entry was not saved")        }

Any help in trying to understand this would make me very appreciative. 

A:

That's because you are re-saving the same object attributes each time you make save(). You should create a new entity for each object with this method:
let newobject = NSEntityDescription.entityForName("my_entity", inManagedObjectContext: context)
date = myTimeZoneDateFormatter1.dateFromString("2016-02-03")!
newobject.setValue("3.1", forKey: "distanceOfRun")
newobject.setValue(date, forKey: "dateOfRun")

And then save the context again.

