1
我必須更新核心數據中的一些記錄。這是正確的方式嗎?Objective 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中。
如果我錯了,請指點我正確的方式! :)