Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-18-2009, 09:22 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 1
ob1jabroni is on a distinguished road
Default Search Very Slow for NSMutableArray

Hello All,

This is my first post so please be gentle :-)

I have been using this code that I learned from the Beginning iPhone SDK Development Book (Great Book By the way) They had a section in there where they showed how to load a UiTable from data in a plist.

That worked fine, and I added some search capabilities that were also found in the book. While this works very well for plist files that are relatively small, once they get larger, the searching slows down considerably.

Now I have been thinking of converting this to a SQL Lite database, but I was wondering of there was a better way I can search using the existing plist data (That has been loaded into an NSMutableArray.)

By just looking at this, it looks like the main reason it is slow is because it is looping through everything twice. I was hoping someone could provide some guidance as who to make this faster.

Any suggestions would be greatly appreciated.

Here is the Code Snippet that does the search.


Code:
-(void)handleSearchForTerm:(NSString *)searchTerm
{
		 
	 NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
	 [self resetSearch];
	 
	 for (NSString *key in self.keys) {
	 NSMutableArray *array = [names valueForKey:key];
	 NSMutableArray *toRemove = [[NSMutableArray alloc] init]; 
	 
	 for (NSDictionary *item in array){
	 if ([[item objectForKey:@"title"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location ==NSNotFound)
	 [toRemove addObject:item];
	 }
	 
	 if ([array count] == [toRemove count])
	 [sectionsToRemove addObject:key];
	 
	 [array removeObjectsInArray:toRemove];
	 [toRemove release];
	 }
	 
	 [self.keys removeObjectsInArray:sectionsToRemove];
	 [sectionsToRemove release];
	 [table reloadData];	
}
ob1jabroni is offline   Reply With Quote
Old 11-18-2009, 10:04 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Out of curiosity, what does resetSearch do? Aside from the obvious - I mean how does it reset the names array? If it reloads the whole thing from a file that's pretty slow.

If true, I'd suggest loading the file only once, and changing this code to create a displaySections array and a displayNames dictionary by *adding* items, instead of copying everything and then removing non-hits like you do now.

If this is a large data set, then going to SQL would be easier on memory in addition to faster.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, plist, search, uitableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 341
13 members and 328 guests
akacaj, alexP, ClerurcifeDer, Duncan C, givensur, glenn_sayers, GraffitiCircus, guusleijsten, JmayLive, NetGuru, Paul Slocum, Punkjumper, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:45 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0