NSURLConnection and keep-alive - iphone

I have a small bug in my client app that uses NSURLConnection. I have tracked it down to an unexpected connection keep-alive that seems to confuse the web server (probably a bug on the server side). The workaround would be to force-close all outstanding connections at a certain point. Can I do this somehow with NSURLConnection, i.e. something like
[NSURLConnection closeAllIdleConnections];

ASIHTTPRequest has an expirePersistentConnections method. It may do what you're looking for.
It's not a drop-in replacement for NSURLConnection, but it's not too hard to port code from NSURLConnection to ASIHTTPRequest.

I think you might want to look at the following method defined for NSURLConnections. This assumes that you made the call asynchronously. If it isn't an async call, then it probably should be.
cancel Cancels an asynchronous load of
a request.
(void)cancel
Discussion Once this method is called,
the receiver’s delegate will no longer
receive any messages for this
NSURLConnection.
Hope this helps. Andrew.

Related

iPhone: Asynchronous HEAD method

I am very new here. I am trying to update the content of a file from a webserver only if it has changed. I found an example of code here using a synchronous connection in NSURLconnection.
Is it possible to do asynchronously?
NSURLConnection has the method sendSynchronousRequest:returningResponse:error: for synchronous connection. It also has a method sendAsynchronousRequest:queue:completionHandler: for async connection. You need to create a block for completionHandler parameter, which handles the response from web server.
Also:
initWithRequest:delegate:
initWithRequest:delegate:startImmediately:
start
are available for async use. You gonna need to implement some methods of NSURLConnectionDelegate to handle the async response.
Please refer to NSURLConnection Class Reference and NSURLConnectionDelegate Protocol Reference.
i think ASIHTTPRequest is the easiest solution:
set the request Method for your ASIHTTPRequest
// HTTP method to use (eg: GET / POST / PUT / DELETE / HEAD etc). Defaults to GET
NSString *requestMethod;
or you can use a download cache
ASIHTTPRequest can automatically store downloaded data in a cache for use later. This can be helpful in many situations...

iOS - Is it possible that [NSData dataWithContentsOfURL:url] would return only part of the bytes of the content?

Will [NSData dataWithContentsOfURL:url] either return the full amount of bytes on success, or nil if something goes wrong?
Is there a chance, that it would return maybe only half of the bytes of the content... perhaps if their internet connection fails halfway through?
If there is a chance that it would return only partial data, is there some other function I could use that would be more reliable and I would be able to know definitely whether they got the full amount of data or not?
I'm not sure about the implementation of -dataWithContentsOfURL: but using a sychronous method like this is not really recommended anyway.
Something based on NSURLConnection is your best bet, but you need to be aware of a few things. Most people don't realize that if the server disconnects while an NSURLConnection is receiving data, it will not cause the download to fail with an error. The -connectionDidFinishLoading: delegate method will be called as normal. Many people get this wrong.
If you want to be sure you have all the data, you need to handle the -connection:didReceiveResponse: delegate method and save the value of [response expectedContentLength]. Then in -connectionDidFinishLoading: you can make sure you received the same number of bytes as expected, and generate an error if not.
There are many free libraries out there based on NSURLConnection like AFNetworking. However you need to beware of bad code. I've just checked the source to AFNetworking and it appears they also do not check for the case where the server sends back less data than the Content-Length header specifies. Also note that the popular ASIHTTPRequest is no longer being actively developed and has received some criticism over its implementation.
I'll leave it up to others to suggest other alternative libraries, but NSURLConnection is the right direction.
If you 'worry' about such a thing I'd recommend using NSURLConnection with it's appropriate delegates.
The async approach (that is NSURLConnection) is imho always better.

How can I chain asynchronous NSURLConnections?

What would be the most appropriate way to chain asynchronous NSURLConnections? For example, I need to register a user with a web service. The workflow would be as follows:
Get Register Token => Use token to Register => Get Login Token => Use token to Login.
I know how to create an asynchronous NSURLConnection, but I'm unsure how to make sure the previous connection has finished before moving on. I know I can achieve this using NSURLConnection sendSynchronousRequest but I don't like my main thread being blocked while these chained requests happen.
We did EXACTLYA this when we built our first version of SignMeOut for iPhone. We created a subclass of NSUrlconnection and gave it an identifying tag do in the connectionDidFinish you would be able to use a simple switch/case with an enum. Works great - you can see the whole flow and example and code in our blog
http://www.isignmeout.com/multiple-nsurlconnections-viewcontroller/
UPDATE
I've modified the NSURLConnection subclass into a much simpler Category. Most of the usage is the same but cleaner using the same class
https://github.com/Shein/Categories
You can look at connectionDidFinishLoading to start another asynchronous connection. For the conditions as to which connection ended, you can keep references to the connections in case other connections are also expected to finish(probably not needed in your case). Make your class follow the NSURLConnectionDelegate and fire the appropriate connections in the connectionDidFinishLoading delegate method. You can also use the connectionDidReceiveData: method to check for credentials, etc received from the service. Go through the documentation too.
You have different options:
create a queue using a mutable array or dictionary
create an NSOperationQueue kind of easy if you use it in combination
with the new only ios5 API for NSUrlConnection
third parties lib such as AFNetworking
Block GCD grouping them (hard for NSRunLoop reasons, pay attention in wich thread the connection is running in)

NSURLConnection sendSynchronousRequest - is it possible to implement without leak

Is there a way to implement NSURLConnection without it leaking? A number of Apps including NYTimes and others (including mine) suffer from this. Anyone have a working implementation?
It appears that best practice is to use NSURLConnection asynchronously.
According to the documentation, +[NSURLConnection sendSynchronousRequest:returningResponse:error:] is built on top of the asynchronous loading code made available by NSURLConnection. It would not be difficult to reimplement this by spawning and blocking on an NSThread, running the request asynchronously in the background on a run loop and ending the thread once either connectionDidFinishLoading: or connection:didFailWithError: is received.
Of course, you are better off using the asynchronous code in the first place; it makes for a much better user experience

Small, simple examples of asynchronous transmission in iPhone

I am trying to work on application in which images should be loaded asynchronously.
Are there any examples related this?
I tried pokeb-asi to understand asynchronous transmission, and it works within that application fine.
But I want to know how to do it from scratch
I am facing following problems.
I don't know the exact way to do it?
what kind of files / frameworks should be added?
The ASIHTTPRequest class is a subclass of NSOperation, so it's typically used by adding the request to either an NSOperationQueue or an ASINetworkQueue. Each request is pulled off the queue, run in a background thread, and then the delegate for that request is notified via a callback method on the main thread.
You could wrap NSURLConnection in an NSOperation and achieve the same thing, however NSURLConnection has an asynchronous API which may suit your needs a bit better.
Try:
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions