开发者

Objective C - Core Data record update

i have to update some records in Core Data. Is this the right way?

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=[[arr开发者_StackOverflow中文版ay objectAtIndex:i]objectForKey:@"poiID"];
      p.lastmod=[[array objectAtIndex:i]objectForKey:@"lastmod"];
   }
 }
}

NSError *error;
if (![context save:&error]) {
NSLog(@"Errore durante il salvataggio: %@", [error localizedDescription]);
}

I pass an array to my method, and i take the data that already are in CoreData and put it in poiFromCD.

Please, if i had wrong, point me to the right way! :)


No, this is not the standard way to update Core Data objects. You are clearly trying to treat Core Data as SQL and that won't work.

Core Data is not SQL. Entities are not tables. Objects are not rows or records. Attributes are not columns. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.

I can't quite tell what you are trying to do without knowing what your data model looks like but you very rarely have to step through arrays of objects like this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜