This code raises the \"CoreData: error: (19) PRIMARY KEY must be unique\" error.
The Day
entity has only a when
attribute which is an NSDate<
I guess you don't need to insert a new Day
if you already have it (this is the case when day is not nil). In particular I'm referring to [moc insertObject:day]
.
If you use insertNewObjectForEntityForName
, that method inserts the object for you when you save the moc. If you need to modify it (you have retrieved a non-nil day) modify it and save. In additon, I will do processPendingChanges
when the loop finishes (just for performance reasons).
Hope that helps.