我有一个私有NSManagedObjectContext(self.moc)与父另一个私有NSManagedObjectContext。 运行代码时,它在我的设备上运行良好。然而,其中一个beta测试者抱怨了一个问题,当我查看他发给我的日志时,我注意到代码总是在运行一些它根本不应该执行的东西!
//operations start from here
var lastModifiedDate: NSDate = self.dayLog.lastModified
//...do some operations (all the operations here don't touch the self.dayLog)
//saving the self.moc
self.moc.refreshObject(self.dayLog, mergeChanges: true)
if lastModifiedDate != self.dayLog.lastModified {
//most probably we shouldn't get here more than once
lastModifiedDate = self.dayLog.lastModified
//...restart operation from beginning
//One of the beta tester is always running this chunk
}
请注意,DayLog是NSManagedObject,DayLog.lastModified是CoreData日期属性,默认值为01/01/2001。