UISearchbar analysis delay

I have a UISearchbar in my application. This is a dynamic search, and when a user enters text, the remote database is scanned through a remote API call (I think it is through REST).

The table display is updated dynamically as the user enters. I am using NSXMLParser to parse XML results. (so there are 3 delegate methods; didStartElement, didEndElement)

In some cases, the results show duplicate entries, for example, If the user typed YAH, he shows YAHOO 3-4 times. I'm not sure why.

How to reduce the number of times that parsing is performed, or how to delay parsing so that it does not make a request for each character entered / deleted by the user.

This, I suppose, can solve the problem.

+4
source share
2 answers

One thing you can do is to enter a delay before sending a remote API call instead of sending one request for each character.

// Whenever UISearchbar text changes, schedule a lookup - (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)text { // cancel any scheduled lookup [NSObject cancelPreviousPerformRequestsWithTarget:self]; // start a new one in 0.3 seconds [self performSelector:@selector(doRemoteQuery) withObject:nil afterDelay:0.3]; } 
+10
source

Below are the relevant parts of the method that I use in one of my applications to remove duplicates from a web service result.

 NSMutableArray *mutableResults = [[myResults mutableCopy] autorelease]; NSMutableSet *duplicates = [NSMutableSet set]; NSMutableIndexSet *indexesToRemove = [NSMutableIndexSet indexSet]; for (NSString *result in mutableResults) { if (![duplicates containsObject:result]) [duplicates addObject:result]; else [indexesToRemove addIndex:[mutableResults indexOfObject:object]]; } [mutableResults removeObjectsAtIndexes:duplicates]; return mutableResults; 
0
source

Source: https://habr.com/ru/post/1337815/


All Articles