According to Apple documentation ( link ) -
There are many situations where you may need to find existing objects (objects already stored in storage) for a set of discrete input values. A simple solution is to create a loop, then for each value, in turn, select to determine whether the object is preserved, etc. This template does not scale well. If you profile your application with this template, you usually find that fetching is one of the more expensive operations in a loop (compared to just iterating over a collection of items). Even worse, this pattern is a O(n)problem O(n^2).
It is much more efficient - when possible - to create all managed objects in one pass, and then fix any relationship in the second pass. For example, if you import data that, as you know, does not contain any duplicates (say, because your original dataset is empty), you can simply create managed objects to represent your data and not do any searches at all. Or if you import “flat” data without relationships, you can create managed objects for the entire set and filter out (delete) any duplicates before saving, using one large INpredicate.
Question 1: Given that my data that I import does not have any relationship, how can I implement what is described in the last line.
, , , - , , . , , . 100 2000 , ( ). , 100 000 , .
IN Core Data .
:
NSArray *employeeIDs = [[listOfIDsAsString componentsSeparatedByString:@"\n"]
sortedArrayUsingSelector: @selector(compare:)];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:
[NSEntityDescription entityForName:@"Employee" inManagedObjectContext:aMOC]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"(employeeID IN %@)", employeeIDs]];
[fetchRequest setSortDescriptors:
@[ [[NSSortDescriptor alloc] initWithKey: @"employeeID" ascending:YES] ]];
NSError *error;
NSArray *employeesMatchingNames = [aMOC executeFetchRequest:fetchRequest error:&error];
: , . , , :
. , . : .
2: , . , , , , O(n) . Apple , , O(n^2). kth , k . O(nC2) = O(n^2).
, , Apple , , , O(n^2). , ; .
, , - 100.