我正在尝试使用默认数据填充核心数据以进行测试,但是当我尝试通过获取请求访问它们时,创建的25个DataPoint对象不会将Meal对象存储在其.meal属性中。在保存之前,我已遍历DataPoint对象并打印了他们的.meal属性,该属性显示已成功为每个属性设置属性。但是,在我获取这些DataPoints时的保存之后,只有最后一个DataPoint包含其中的膳食设置。
if User.currentUser == nil {
let newUser = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: managedObjectContext) as! User
newUser.username = "Will"
newUser.avatar_large = UIImagePNGRepresentation(UIImage(named: "Karl@3x.png")!)
newUser.meals = NSSet()
let newMeal = NSEntityDescription.insertNewObjectForEntityForName("Meal", inManagedObjectContext: managedObjectContext) as! Meal
newMeal.title = "BURGER AND FRIES"
newMeal.time = NSDate().dateByAddingTimeInterval(-24*60*60)
newUser.meals = newUser.meals?.setByAddingObject(newMeal)
var last = 150.0
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
for i in 0..<25 {
let dataPoint = NSEntityDescription.insertNewObjectForEntityForName("DataPoint", inManagedObjectContext: managedObjectContext) as! DataPoint
dataPoint.time = newMeal.time.dateByAddingTimeInterval(Double(i) * 300.0)
last += drand48() * 40 * (drand48() > 0.5 ? -1.0 : 1.0)
dataPoint.value = NSNumber(double: last)
dataPoint.meal = NSSet(object: newMeal)
}
do {
try managedObjectContext.save()
} catch {
print("User Save Failed")
}
}