0

In y iPhone App.

For search functionality. I am using UISearchBar and WebService call.

Whenever UISearchBar 'TextDidChange' happen the web service call happens.

Generally we are typing very fast, so there are many web service call happens, and I am using NSURLConnection, and I am loading table on Finished Loading.

eg, WebService Call for M WebService Call for Mo WebService Call for Mor

Here, the problem is one webservice is going to finish, in between another web service called. This makes chaos.

Here, I solved the problem with writing.

**[connectionSearch cancel];**   

        connectionSearch=[[NSURLConnection alloc] initWithRequest:request delegate:self];
Arpit B Parekh
  • 1,932
  • 5
  • 36
  • 57

2 Answers2

2

Write this code in textDidChange method for calling Webservice

[NSObject cancelPreviousPerformRequestsWithTarget:self];  
[self webserviceCallMethod];  

You can also call service like below after cancelPreviousPerformRequestsWithTarget:self

[self performSelector:@selector(webserviceCallMethod:) withObject:searchText afterDelay:0.3f]; // after delay can be an anything that helps.  

Don't forget to remove all objects from your array, before you add objects to array again in response of your webservice. (Array that is used in cells of UITableView)

Hope it helps.

Milan Gupta
  • 1,181
  • 8
  • 21
  • I have also two different kind web servive calls, at a same time. So, will it work NSObject cancelPreviousRequest, Instead I have done something similar. See my answer. – Arpit B Parekh Aug 13 '16 at 05:50
  • 1
    okay, nice to know new ways. Just for your info apps with `NSURLConnection ` will not be approved by Apple anymore. Refer : http://stackoverflow.com/a/37693532/5012384 – Milan Gupta Aug 13 '16 at 05:58
  • It is supported, as mentioned in your link. See the first answer in your post. And thanks for sharing the above link. – Arpit B Parekh Aug 13 '16 at 06:27
0

I changed the code like,

  **[connectionSearch cancel];**   

  connectionSearch=[[NSURLConnection alloc] initWithRequest:request delegate:self];

So, whenever the same Search web service call happens, and if the call is not finished, call is cancelled and the new web service call happens.

Arpit B Parekh
  • 1,932
  • 5
  • 36
  • 57