更新核心数据(如果存在)或创建新的托管对象。我可以让这个更快吗?

发布于 2024-10-13 13:46:51 字数 1561 浏览 6 评论 0原文

我有以下代码,我想知道是否有任何方法可以使其更快。基本上,我的应用程序从网络下载一些 JSON(大约 4000 条记录),并根据数据更新或创建我的托管对象。目前它非常慢,我知道为什么,但我对核心数据很陌生,所以我想知道是否有什么办法可以让它更快?

 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
 [request setEntity:entity];
 NSMutableArray *coreDataArray = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
 [request release];

 for (NSDictionary *dict in arr) {
  NSArray *filtered = [coreDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(code == %@)", [dict objectForKey:@"Code"]]];
  //NSLog(@"COREDATA ARRAY: %d FILTERED ARRAY: %d CODE: %@ COREDATA FIRST CODE: %@", [coreDataArray count], [filtered count], [dict objectForKey:@"Code"], [[coreDataArray objectAtIndex:0] code]);
  if ([filtered count] > 0) {
   Company *c = [filtered objectAtIndex:0];
   if ([dict objectForKey:@"Defunct"]) {
    NSLog(@"DELETED DEFUNCT COMPANY");
    [managedObjectContext deleteObject:c];
   } else {
    [c populateWithJSONDictionary:dict];
   }
  } else {
   Company *c = (Company *)[NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:managedObjectContext];
   [c populateWithJSONDictionary:dict];
  }

  float percent = (float)[arr indexOfObject:dict]/[arr count];
  [self performSelectorInBackground:@selector(updateProgressView:) withObject:[NSString stringWithFormat:@"%f",percent]];
 }

 [coreDataArray release];

非常感谢您提供的任何帮助。

I have the following code, and I was wondering if theres any way to make this faster. Basically my app downloads some JSON (about 4000 records) from the net, and updates or creates my managed objects based on the data. At the moment it's quite slow, and I can see why, but I'm new to core data so I was wondering if there's anything I can do to make it faster?

 NSFetchRequest *request = [[NSFetchRequest alloc] init];
 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Company" inManagedObjectContext:managedObjectContext];
 [request setEntity:entity];
 NSMutableArray *coreDataArray = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
 [request release];

 for (NSDictionary *dict in arr) {
  NSArray *filtered = [coreDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(code == %@)", [dict objectForKey:@"Code"]]];
  //NSLog(@"COREDATA ARRAY: %d FILTERED ARRAY: %d CODE: %@ COREDATA FIRST CODE: %@", [coreDataArray count], [filtered count], [dict objectForKey:@"Code"], [[coreDataArray objectAtIndex:0] code]);
  if ([filtered count] > 0) {
   Company *c = [filtered objectAtIndex:0];
   if ([dict objectForKey:@"Defunct"]) {
    NSLog(@"DELETED DEFUNCT COMPANY");
    [managedObjectContext deleteObject:c];
   } else {
    [c populateWithJSONDictionary:dict];
   }
  } else {
   Company *c = (Company *)[NSEntityDescription insertNewObjectForEntityForName:@"Company" inManagedObjectContext:managedObjectContext];
   [c populateWithJSONDictionary:dict];
  }

  float percent = (float)[arr indexOfObject:dict]/[arr count];
  [self performSelectorInBackground:@selector(updateProgressView:) withObject:[NSString stringWithFormat:@"%f",percent]];
 }

 [coreDataArray release];

Many thanks for any help you can give.

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

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

发布评论

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

评论(1

我不在是我 2024-10-20 13:46:51

您应该查看核心数据编程指南:性能 部分

它对数据导入性能有一些具体的建议。

如果苹果再次移动文档,这里是 Google site:developer.apple.com 核心数据导入性能上的一个很好的搜索查询

You should check out the Core Data Programming Guide: Performance section

It has some specific advice for data import performance.

In case Apple moves the documentation again, here is a good search query on Google site:developer.apple.com core data import performance

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