I am trying to find the index of a core data record using the following code, where [self numItems] returns the number of records. The code takes about 0.2 seconds on average to execute. I am interested in speeding it up. Thanks in advance.
- (NSInteger) indexWithTaskID:(NSInteger)eventID
{
for (NSInteger i=0;i<[self numItems];i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSManagedObject *managedObject = [self.fetchedResultsController
objectAtIndexPath:indexPath];
NSInteger myEventID = [(NSNumber *)[managedObject valueForKey:@"eventID"] intValue];
if (myEventID == eventID) {
return i;
}
}
return -1;
}
This is a little bit backward of what this is setup for, I’d try to avoid needing to find this index if you have much data, but if you really need to know the index then you might be stuck doing this search. If the predicate is sorted, you could do quite a bit better then a linear search, maybe a simple binary search could make enough improvement.