Stop current NSURLConnection

2020-07-03 07:55发布

问题:

Currently, I am making an NSURLConnection whenever mySearchBar.text did change. I am experiencing an issue whereby if the user enters text too quickly, The respondData gets corrupted.

So, I was wondering is there a way to check whether the same NSURLConnection is still loading? If yes, How do I drop the current connection and start a new one?

回答1:

Add a property for that NSURLConnection and before starting new connection, do:

[self.conn cancel];
self.conn = nil;


回答2:

You could try to use asynchronous request. A good library for such call is ASIHTTPRequest.

set [request cancel]; whenever you need to make another new request.