有没有更有效的方法来处理这个?
任何建议或指示将不胜感激.
- (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的有效方法.所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)