【发布时间】:2018-04-07 04:31:23
【问题描述】:
我在core-data 中有一个名为Record 的实体。问题是我在操作后无法保存对象,如下所示:
extension Records {
@nonobjc public class func createFetchRequest() -> NSFetchRequest<Records> {
return NSFetchRequest<Records>(entityName: "Records")
}
@NSManaged public var datetime: Date
@NSManaged public var year: Int64
@NSManaged public var month: Int16
public override func willSave() {
super.willSave()
if (self.datetime != nil) {
self.year = Int64(datetime.year())
self.month = Int16(datetime.month())
}
}
}
extension Date {
func month() -> Int {
let month = Calendar.current.component(.month, from: self)
return month
}
func year() -> Int {
let year = Calendar.current.component(.year, from: self)
return year
}
}
这是我遇到的错误信息:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to process pending changes before save. The context is still dirty after 1000 attempts. Typically this recursive dirtying is caused by a bad validation method, -willSave, or notification handler.
【问题讨论】: