Why is this code raising the “CoreData: error: (19) PRIMARY KEY must be unique” error?

前端 未结 2 1145
梦毁少年i
梦毁少年i 2020-12-30 15:07

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<

2条回答
  •  离开以前
    2020-12-30 15:28

    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.

提交回复
热议问题