私は Core Data をまったく使用したことがなく、理解できない問題を抱えています。何が問題なのかわかりません。
次のコードを使用して、JSON ファイルから読み取られるエンティティ「Weight」の 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);
}
取得した各オブジェクトの 1 つの属性を表示しようとすると、TableView に 1044 行が表示されます!! 私がちょうど7を持っているべきとき。
私は何を間違っていますか?問題は保存時ですか、それとも取得時ですか?
この問題の解決にお役に立てば幸いです。よろしくお願いします!