Reputation: 12684
I have an app that makes moderate use of NSURLConnection. These async calls eventually finish and release properly (it looks like), but sometimes it takes some time for them to finish.
So, there are times when I exit the app, (note, not just sending it the background), that some of these connections are still active. If I immediately restart the app, the app freezes on startup. (didFinishLaunchingWithOptions never seems to get called).
While I'm not certain these connections are the issue, it would probably be good to terminate or cancel any remaining. Any suggestions on how to do this?
Bonus points on how to debug the restart also. (I'm already saving NSLog statements to a downloadable file)
Upvotes: 2
Views: 5215
Reputation: 25968
For cancel last request : [NSURLConnection cancelPreviousPerformRequestsWithTarget:self];
For cancel any specific request : [request cancel]; Note: Here request is the instance of NSURLConnection
Upvotes: 0
Reputation: 18670
You can cancel any NSURLConnection by sending it a cancel command.
[connection cancel];
From Apple docs
Cancels an asynchronous load of a request. Once this method is called, the receiver’s delegate will no longer receive any messages for this NSURLConnection.
Your start up issue could be related but hard to tell without knowing what type of data you're downloading and how you are using it.
Upvotes: 7
Reputation: 3415
Make sure you are calling release on the connection. Maybe it doesn't call shutdown or close on the socket until the last reference is dropped. I do something like this with no issues with references.
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
Upvotes: 0