1
コアデータの一部のレコードを更新する必要があります。これは正しい方法ですか?目的C - コアデータレコードの更新
NSManagedObjectContext *context = [self managedObjectContext];
NSMutableArray *poiFromCD=(NSMutableArray *)[self takePoiFromCoreData];
for (int i=0; i<[array count]; i++) {
for (int j=0; j<[poiFromCD count]; j++) {
Poi *poi=(Poi *)[poiFromCD objectAtIndex:j];
if ([[[array objectAtIndex:j]objectForKey:@"poiID"]isEqualToString:poi.poiID]) {
Poi *p=[poiFromCD objectAtIndex:j];
p.poiID=[[array objectAtIndex:i]objectForKey:@"poiID"];
p.lastmod=[[array objectAtIndex:i]objectForKey:@"lastmod"];
} else {
Poi *p;
p=[NSEntityDescription insertNewObjectForEntityForName:@"Poi" inManagedObjectContext:context];
p.poiID=[[array objectAtIndex:i]objectForKey:@"poiID"];
p.lastmod=[[array objectAtIndex:i]objectForKey:@"lastmod"];
}
}
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Errore durante il salvataggio: %@", [error localizedDescription]);
}
私は自分のメソッドに配列を渡して、すでにCoreDataにあるデータを取り出してpoiFromCDに入れます。
私が間違っていたら、正しい方法を教えてください! :)