[英]Related data save Coredata
I am trying save a data into core data. 我正在尝试将数据保存到核心数据中。 ( Main model and related model).
(主要模型和相关模型)。 When I save after kill main model is saved but doesn't related model saved.
当我杀死后保存时,主要模型已保存,但未保存相关模型。 I have a following code:
我有以下代码:
NSManagedObjectContext *context = [self managedObjectContext];
Trace *trace = [NSEntityDescription
insertNewObjectForEntityForName:@"Trace"
inManagedObjectContext:context];
trace.totalDistance = self.totalDistance;
trace.averageSpeed = self.averageSpeed;
trace.theBestSpeed = self.theBestSpeed;
trace.stopTime = [NSNumber numberWithInt: self.stopTime];
trace.time = self.time;
NSMutableArray *nsa = [[NSMutableArray alloc] init];
for (int i=0;i<locations.count;i++){
CLLocation* cl =(CLLocation*) locations[i];
Argument *point = [NSEntityDescription
insertNewObjectForEntityForName:@"Argument"
inManagedObjectContext:context];
point.latitude = [NSNumber numberWithDouble: cl.coordinate.latitude];
point.langitude = [NSNumber numberWithDouble: cl.coordinate.longitude];
point.speed = self.speeds[i];
[nsa addObject:point];
}
NSOrderedSet * ns = [[NSOrderedSet alloc] initWithArray:nsa];
trace.points = ns;
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
When I kill an application Arguments don't saved , but Trace is saved. 当我杀死一个应用程序时,不会保存参数,但是会保存跟踪。 Shat can cause the problem??
会导致问题吗??
Quite simply, when you kill the app before it saves, it doesn't save. 很简单,当您在保存应用程序之前将其杀死时,它不会保存。 No surprises.
没什么好奇怪的
First - make sure you're using entity relationships the correct way. 首先-确保您以正确的方式使用实体关系。
In xcode model editor select your entity, go down to Relationships and make sure you have a 'To-Many' relationship to your Argument object. 在xcode模型编辑器中,选择您的实体,转到“关系”,并确保您与Argument对象具有“多对多”关系。 Don't use attribute to link your objects, that won't work.
不要使用属性来链接您的对象,这是行不通的。
After doing that (you can check the images below) you associate new objects the the 'Trace' object using [traceObject addArgumentsObject:argument]
and remove them the same way. 之后(您可以查看下面的图像),然后使用
[traceObject addArgumentsObject:argument]
将新对象与“ Trace”对象相关联,并以相同的方式将其删除。
For further check the Core Data Programming guide 有关更多信息,请参阅《 核心数据编程指南》
Model - 型号-
Property of the relationship - 关系的属性-
Aft 尾部
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.