我有一个包含200,000个项目的 JSON对象.我需要迭代这些对象,并确定它们是否存在并执行相关操作(插入/更新/删除).这个shell如下所示.当然,它实际上并没有保存任何东西.更多的是看这种方
有没有更有效的方法来处理这个?
任何建议或指示将不胜感激.
- (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数据的“许多”产品代码数组.当然,您必须找到最佳的“批量大小”.