简体   繁体   中英

cancel file uploading with NSURLConnection

I've got NSURLConnection with timeout = 30s which is uploading an image on server. If connection is horrible and call delegate method didFailWithError: then i need to cancel current connection. But if i just call the [myConnection cancel] connection will still alive but will not call delegates methods (apple docs say it - NSURLConnection cancel method). And i want to abort connection but not only remove delegate methods. How i can do what?

upd: My problem is if connection is fails by timeout - in business logic i must recreate connection with similar request. If i have got horrible connection for 1 min and after that connection will be good - server will get a lot of (about 3 times retry count) photos. But first 2 connections is canceled.

At the moment i make "dirty hack" like "if it's photo uploading request" - do not retry recreate connection.

I do a ton of network stuff and don't recall a scenario where everything was successfully received but the iOS app timed out. I'm trying to grok the scenario you describe, where you're seeing this happen a lot and I'm not seeing how that would happen. We might need to see some of your code.

Regardless, when you cancel a NSURLConnection , it not only stops the delegate methods from being called, but it stops the upload, too. I just did a test:

  • I attempting to upload a 20mb file (non- chunked request);

  • At the 1mb mark (as identified by didSendBodyData ), I canceled the connection (by calling [connection cancel] );

  • I immediately stopped receiving any delegate messages at that point;

  • Looking at Charles, I'm only seeing 1.3mb of data in the hex log of the request. When I look at the "Network" tab of the Mac OS "Activity Monitor" and looking at by "Sent Bytes", it's at 2.1mb uploaded.

So canceling a connection will stop further data from being sent. Perhaps if there is some transmission in progress that still gets out (that's the asynchronous world we live it), but the it's not true to conclude that canceled connections will routinely send their full HTTP request. There must be something about the nature of the timeout that is unique to your environment.

In terms of your immediate problem, I might suggest that when uploading a file that the iOS app assign some unique identifier to the upload so that the server code can immediately recognize duplicate requests and handle them appropriately. But the question is why you are seeing so many time-outs and notably ones where the request appears to be successfully received in toto, but the response is not. That's very curious.

You cannot forcefully abort an ongoing connection. In case if connection is not yet started cancel and unscheduleFromRunLoop for the NSURLConnection will work.

Try with following step

[myConnection cancel];
myConnection = nil;

Might be helpful in your case and If this step is not working then also try with myConnection.delegate = nil;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM