Search code examples
iossearch-engineuiactivityindicatorview

UIActivtyIndicator while searching


I'm making an app that searches through a dictionary after the user presses the search button that appears on the pop-up keyboard. Since it takes some time to search through the dictionary, I wanted to add an UIActivtyIndicator. The problem is just that i don't know where I should add the following line of code:

[loader startAnimating];

Because if I add it in the UITexfieldDelegateProtcol method (see the code below), the keyboard would just freeze for a couple of seconds while performing the search. The keyboard would then remove itself and the app would show the results without the UIActivityView showing up.

- (BOOL)textFieldShouldReturn:(UITextField *)textField

    [textField resignFirstResponder];
    [loader startAnimating]; //loader is the name of the activityindicator
    [self findSearchWords]; //Starts the dictionary search

return YES;

If I would delete the following line:

[self findSearchWords];

Then the UIActivtyIndicator would show up and start spinning, but a search would of course not be performed.

My question is therefore, where should I implement the UIActivityIndicator's method "startAnimating", so that it will show up and spin while the search is performed. I've also tried implementing the method (startAnimating) in other UITextFieldDelegateProtocol methods without any success!


Solution

  • If you would like to add activity indicator you should perform search in separate thread. That is because of you will add activity indicator in the main thread and if you perform search in the same thread activity won't appear because it would be locked by your search process.