ios – 在CoreData中执行批量INSERTUPDATEDELETE的有效方法.

ios – 在CoreData中执行批量INSERTUPDATEDELETE的有效方法.,第1张

概述我有一个包含200,000个项目的 JSON对象.我需要迭代这些对象,并确定它们是否存在并执行相关 *** 作(插入/更新/删除).这个shell如下所示.当然,它实际上并没有保存任何东西.更多的是看这种方式需要多长时间.考虑到甚至还没有发生任何变化,这个动作在iPhone 4上处理大约需要8分钟,看起来很疯狂. 有没有更有效的方法来处理这个? 任何建议或指示将不胜感激. - (void) progres 我有一个包含200,000个项目的 JSON对象.我需要迭代这些对象,并确定它们是否存在并执行相关 *** 作(插入/更新/删除).这个shell如下所示.当然,它实际上并没有保存任何东西.更多的是看这种方式需要多长时间.考虑到甚至还没有发生任何变化,这个动作在iPhone 4上处理大约需要8分钟,看起来很疯狂.

有没有更有效的方法来处理这个?

任何建议或指示将不胜感激.

- (voID) progressiveInsert{    prodAdd = 0;    prodUpdate = 0;    prodDelete = 0;    dispatch_queue_t backgrounddispatchQueue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_BACKGROUND,0);    dispatch_async(backgrounddispatchQueue,^{                       _productDBCount = 0;                       NSLog(@"Background Queue");                       NSLog(@"Number of products in JsonArray: %lu",(unsigned long)[_products count]);                       NSManagedobjectContext *backgroundthreadcontext = [[NSManagedobjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];                       [backgroundthreadcontext setPersistentStoreCoordinator:_persistentStoreCoordinator];                       [backgroundthreadcontext setUndoManager:nil];                       [fetchRequest setPredicate:predicate];                       [fetchRequest setEntity:[NSEntityDescription entityForname:@"Products" inManagedobjectContext:_managedobjectContext]];                       [fetchRequest setIncludesSubentitIEs:NO]; //Omit subentitIEs. Default is YES (i.e. include subentitIEs)                       [fetchRequest setFetchlimit:1];                       [_products enumerateObjectsUsingBlock:^(ID product,NSUInteger IDx,BOol *stop) {                           predicate = [nspredicate predicateWithFormat:@"code == %@",[product valueForKey:@"product_code"]];                           [fetchRequest setPredicate:predicate];                           NSError *err;                           NSArray *fetchedobjects = [_managedobjectContext executeFetchRequest:fetchRequest error:&err];                           if (fetchedobjects == nil) {                               if ([[product valueForKey:@"delete"] isEqualToNumber:[NSNumber numberWithBool:TRUE]]){                                   prodDelete += 1;                               } else {                                   prodAdd += 1;                               }                           } else {                               if ([[product valueForKey:@"delete"] isEqualToNumber:[NSNumber numberWithBool:TRUE]]){                                   prodDelete += 1;                               } else {                                   prodUpdate += 1;                               }                           }                           dispatch_sync(dispatch_get_main_queue(),^                                         {                                             self.productDBCount += 1;                                             float progress = ((float)self.productDBCount / (float)self.totalCount);                                             _downloadProgress.progress = progress;                                             if (_productDBCount == _totalCount){                                                 NSLog(@"Finished processing");                                                 _endProcessing = [NSDate date];                                                 [_btn.TitleLabel setText:@"Finish"];                                                 NSLog(@"Processing time: %f",[_endProcessing timeIntervalSinceDate:_startProcessing]);                                                 NSLog(@"Update: %i // Add: %i // Delete: %i",prodUpdate,prodAdd,prodDelete);                                                 [self completeUpdateProcess];                                             }                                         });                       }];                   });}
解决方法 看一下
Implementing Find-or-Create Efficiently在“核心数据编程指南”中.

(更新:当前的核心数据编程指南中不再存在本章.可以在以下位置找到存档版本
http://web.archive.org/web/20150908024050/https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html.)

其中一个关键想法是不对每个产品执行一次获取请求,而是执行一个
带有谓词的“批量提取”

[nspredicate predicateWithFormat:@"code IN %@",productCodes]

其中productCodes是来自JsON数据的“许多”产品代码数组.当然,您必须找到最佳的“批量大小”.

总结

以上是内存溢出为你收集整理的ios – 在CoreData中执行批量INSERT / UPDATE / DELETE的有效方法.全部内容,希望文章能够帮你解决ios – 在CoreData中执行批量INSERT / UPDATE / DELETE的有效方法.所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1018647.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存