I'm using CocoaLibSpotify in my iPhone app. At one point the user has the option to search for artists/songs etc on Spotify. Once the search completes, a UITableView is populated with the search results. I've noticed that the app starts to lag a lot after performing a few searches. I started up the Activity Monitor in Instruments and noticed that CPU usage (% CPU in the Activity Monitor) climbs up to around 200% after performing a search. What's strange is, it sometimes takes up to 2 minutes to come back down to a normal level of about 6%. Here is the code I'm using to perform a search:
-(void)performSearch:(NSString *)searchString{
if(!self.spotifySearch){
self.spotifySearch = [SPSearch searchWithSearchQuery:searchString inSession:[SPSession sharedSession]];
[SPAsyncLoading waitUntilLoaded:self.spotifySearch timeout:20.0 then:^(NSArray *loadedItems, NSArray *notLoadedItems){
NSLog(@"Search completed");
self.spotifySearch = nil;
}];
}
}
Obviously my search method isn't really doing much yet but simply running the above code multiple times causes a massive CPU load. Sometimes it also happens after running the code just once. My questions now are:
- Is there something I'm doing wrong?
- How can I run multiple SPSearches with different search strings? Is re-initializing the SPSearch object with SPSearch searchWithSearchQuery the proper way to do this?
- Is there a way to cancel a running search?
Any help is appreciated, thank you!