【发布时间】:2012-02-18 13:19:36
【问题描述】:
我对 Core Data 很陌生,但我遇到了一个我不明白的问题。我不知道出了什么问题。
我将使用以下代码从 JSON 文件中读取的实体“重量”的 7 个对象保存到我的持久存储中:
for (NSDictionary *values in aWeightValues)
{
weightValues = [NSEntityDescription insertNewObjectForEntityForName:@"Weight"
inManagedObjectContext:moc];
[weightValues setValue:[typeWeight objectForKey:@"unit"] forKey:@"unit"];
[weightValues setValue:[values objectForKey:@"timestamp"] forKey:@"date"];
[weightValues setValue:[values objectForKey:@"value"] forKey:@"amount"];
if (![moc save:&error])
{
NSLog(@"Problem saving: %@", [error localizedDescription]);
}
}
for 循环进行了 7 个循环,这意味着它被正确保存(谈到对象的数量)。
但是,当我尝试以这种方式从持久存储中检索数据时:
-(NSMutableArray *) extractWeightEntities
{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSError *error;
NSManagedObjectContext *moc = [appDelegate managedObjectContext];
NSEntityDescription *entityWeight = [NSEntityDescription entityForName:@"Weight" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc]init]autorelease];
[request setEntity:entityWeight];
entityWeight = nil;
fetchResult = [[moc executeFetchRequest:request error:&error]mutableCopy];
return (fetchResult);
}
并尝试显示检索到的每个对象的一个属性,我的 TableView 中有 1044 行!我应该只有 7 个。
我做错了什么?是我保存还是检索时出现问题?
希望您能帮助解决这个问题。非常感谢提前!
【问题讨论】:
标签: sqlite core-data save nsfetchrequest persistent-storage