Objective C - 核心数据记录更新

发布于 2024-11-04 06:03:49 字数 1064 浏览 5 评论 0原文

我必须更新核心数据中的一些记录。这是正确的方法吗?

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 中。

如果我错了,请指出我正确的方法! :)

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=[[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]);
}

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! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-11-11 06:03:49

不,这不是更新核心数据对象的标准方法。显然,您试图将核心数据视为 SQL,但这是行不通的。

核心数据不是 SQL。实体不是表。对象不是行或记录。属性不是列。 Core Data 是一个对象图管理系统,它可能会也可能不会持久化对象图,并且可能会也可能不会在幕后使用 SQL 来执行此操作。尝试用 SQL 术语来思考 Core Data 将会导致您完全误解 Core Data,并导致很多痛苦和浪费时间。

在不知道数据模型是什么样子的情况下,我无法完全判断您要做什么,但您很少需要单步执行这样的对象数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文