Friday, 27 September 2013

ios activity indicator is visible once, and then will not show until I try again a minute later

ios activity indicator is visible once, and then will not show until I try
again a minute later

Ok, so I have this piece of code:
- (void) searchBarSearchButtonClicked:(UISearchBar *)aSearchBar
{
[_activity startAnimating];
[self.searchBar bringSubviewToFront:_activity];
[self.searchBar resignFirstResponder];
if([_activity isAnimating]){
NSLog(@"its animating!");
}
dispatch_queue_t background =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(background, ^{
[self performSelectorOnMainThread:@selector(filter:)
withObject:aSearchBar.text waitUntilDone:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[_activity stopAnimating];
if(![_activity isAnimating]){
NSLog(@"its not animating anymore!");
}
[self.tableView reloadData];
});
});
}
what i'm trying to do is start an activity indicator which I added as a
subview to the search bar. In this code, I start the animation (logging
for test) on the main thread, then set up a background asynchronous thread
which runs the method that generates the new data set which will reload
the tableview. I wait for this to return, and which point, I stop
animation and reload the tableview on the main thread.
the result is this...when the application runs, I go to the table view,
and I type a word in the search bar. When I hit enter, the activity
indicator shows up, the table loads, and the activity indicator stops.
This is great.
Then, I click on the search bar again, start a new search, only this time,
the activity indicator does not show itself, but the NSLog shows that it
IS animating. The tableview reloads as expected, but no activity indicator
on the UI, but based on the NSLog, it seems to working.
Finally, if I wait about a minute, and search again, it works fine. What
I'm suspecting is that that the background thread is not completing maybe,
and the main thread continues doing what it needs to do.
Honestly, I've been beating my head for two days now, can someone explain
what I'm doing wrong?

No comments:

Post a Comment