0

I have a TextField that is search in my app, I have added the event Editing Changed, but when I type fast CoreData is not able to search so rapidly so the results are not precise.

Currently I'm using such script:

[self->_searchThread cancelAllOperations];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(performSearch:) object:((UITextField *)sender).text];
[self->_searchThread addOperation:operation];

in performSearch I add all the predicates run through results and format them and then call:

[self performSelectorOnMainThread:@selector(setSearchResults:) withObject:@[[NSArray arrayWithArray:data]] waitUntilDone:NO];

I thought using [self->_searchThread cancelAllOperations]; would cancel current request and start a new one immediately but it does not seem to work like that. Is there any way I can cancel the current running search and start new once user changes the TextField, or are there any other solutions?

Thanks.

5
  • Just a question: why aren't you using a UISearchBar with all the delegates for search inputs etc?
    – Alice
    May 15, 2014 at 1:36
  • Because I have customized layout and UISearchBar does not fit in overall layout - I tryed. May 15, 2014 at 1:41
  • Regarding @Alice's comment and your response, you are able to manipulate the size of a UISearchBar with the convenience (initialiser) method initWithFrame:, and because a UISearchBar has a frame property you can adjust the frame dynamically during code execution. May 15, 2014 at 1:48
  • 1
    Regarding your question, have you considered creating an NSArray of all the fetchedObjects from your NSFetchedResultsController instance? I suspect it may be faster to apply your same predicate to such an NSArray using the filteredArrayUsingPredicate method. May 15, 2014 at 1:49
  • That would be bad, because there are a lot of elements and I would use three arrays, because of relationships, but I thinks I found a solution, I put the search thread(NSManagedObject) to sleep for 200ms and right after that I added 'if(isCancelled) return;' So if user types fast the CoreData does not get initialized. May 15, 2014 at 6:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.