我在应用程序中使用核心数据作为本地存储。我已经正确设置了它,并为每个实体创建了NSManagedObject的子类。但是,当我尝试向我的商店中插入值时,它只会插入我的JSON提要中的最后一个对象。仅存储JSON订阅源的最后一个对象的核心数据
res = [JSONHandler requestJSONResponse:jsonString];
shows = [res valueForKeyPath:@"Show.Name"];
NSUInteger showIndex = 0;
for(NSString *showName in shows){
showObject = [NSEntityDescription insertNewObjectForEntityForName:@"Show" inManagedObjectContext:managedObjectContext_];
showObject.name = showName;
showObject.iD = [[res valueForKeyPath:@"Show.Id"]objectAtIndex:showIndex];
showObject.desc = [[res valueForKeyPath:@"Show.Description"]objectAtIndex:showIndex];
showObject.activityType = [[res valueForKeyPath:@"Show.ActivityType"]objectAtIndex:showIndex];
showIndex++;
}
这只存储我的JSON提要中的最后一个对象。任何想法为什么?
编辑:
res = [JSONHandler requestJSONResponse:jsonString];
shows = [res valueForKeyPath:@"Show.Name"];
NSUInteger index = 0;
for(NSString *showName in shows){
show = [NSEntityDescription insertNewObjectForEntityForName:@"Show" inManagedObjectContext:managedObjectContext_];
[show setValue:showName forKey:@"name"];
[show setValue:[[res valueForKeyPath:@"Show.Id"]objectAtIndex:index] forKey:@"iD"];
[show setValue:[[res valueForKeyPath:@"Show.Description"]objectAtIndex:index] forKey:@"desc"];
[show setValue:[[res valueForKeyPath:@"Show.ActivityType"]objectAtIndex:index] forKey:@"activityType"];
index++;
}
It's基本上是一回事,isn't它:当我这样做,它工作正常?但我想使用NSManagedObject的子类而不是像上面那样做。因为在上面的代码片段中显示的是NSManagedObject * show,而不是它应该显示的内容:Show * show。
JSON的外观如何? NSLog的结果是什么(@“显示:%@”,显示);'? –
我100%确定JSON没问题。 – Magnus
如果添加演员,该怎么办? 'showObject =(Show *)[NSEntityDescription insertNewObjectForEntityForName:@“Show”inManagedObjectContext:managedObjectContext _];' – chris