Search code examples
iosuitableviewcore-datansmutablearrayuisearchbar

iOS Need to know what search method is most efficient using CoreData ManagedObjects or NSMutableArrays?


I am currently working the search feature for my app. The app uses CoreData. Now here is my question.

I am having trouble deciding in which method to use, should I use the NSMutableArray method to find the strings in all my data.

NSArray method: This is just to set the idea of what I mean of the search feature with NSArray.

NSArray* inputArray = [NSArray arrayWithObjects:@"dog", @"cat", @"fat dog", @"thing", @"another thing", @"heck here's another thing", nil];

NSMutableArray* containsAnother = [NSMutableArray array];
NSMutableArray* doesntContainAnother = [NSMutableArray array];

for (NSString* item in inputArray)
{
if ([item rangeOfString:@"another"].location != NSNotFound)  
[containsAnother addObject:item];
}

CoreData Method: Still working on it, sorry. I have an idea tho how to implement it.

The thing is, since CoreData works with SQLite, I believe it should be best to use it like supposedly the search will be efficient with it. But that is my opinion. I just wanted to know which method is most efficient since most likely this feature will be used when lots of records begin to be saved.


Solution

  • In general, managed objects with predicates tend to be faster, because they can use the SQL search engine, which can be faster (and more memory efficient).

    In this instance, it looks like you'd have to copy the entire table to memory, then pick out certain items, which is definitely less efficient than simply requesting all items where location is empty. (Of course, the question becomes how do you write a predicate for an empty field, and I'm not comfortable enough with Core-Data yet to comment on that).

    In other words, your question appears to boil down to which is faster: copying all your data from the database, then checking to see which items you want, or just copying out the ones you want. I know which one I'd believe.