views:

1682

answers:

4

I connect asynchronously with server each 5 seconds. The URL is the same, but POST-body is changed each time. Now I create NSURL, NSURLRequest and NSURLConnection from the scratch each time.

I think it'd be more effective to set connection once and just use that one further. I am a newbie and not sure if that possible. There is no mutable NSURLConnection, but may it's need to create NSURLConnection like:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

and change NSMutableURLRequest POST-data to send another request to server. Which way is right?

A: 

make a method that returns a request and do

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:[self requestMethod] delegate:self];

?

tmadsen
[[NSURLConnection alloc] init...]; it creates a new connection each time, but I want use one connection for а long just changing a request's POST-data by:[request setHTTPBody: newRequestData];
slatvick
+3  A: 

I assume what you're concerned about is the overhead of creating the HTTP connection. NSURLConnection is smart enough to handle this for you using HTTP/1.1 and reusing existing connections. It does not use pipelining last time I checked, but for your purpose, connection reuse should be sufficient. I do encourage you to put a network sniffer on this and make sure that it's working as you want them to.

The cost of creating the objects themselves is trivial on the order of once per 5s and you shouldn't try to optimize that (though of course you should reuse the NSURL). It's the opening a connection to the server that's expensive, especially on iPhone.

If you find you really do need pipelining, you unfortunately will have to roll your own. I've heard that CFHTTPStream can do it, but I don't see a lot of evidence of that. CocoaAsyncSocket is your best bet for low-level access to the sockets without having to write low-level code.

Since latency on the cell network can be very bad, it's possible that your connection will take longer than 5s to complete. Do make sure that one connection is done before starting the next, or you'll start making more and more open connections.

Rob Napier
Big thanks. As I understand NSURL opens a connection, right? if so I do not need more optimizations, i just needed to not opening connection each 5 seconds, which is quite expensive, as you understand also.
slatvick
NSURL itself does not open a connection. It just parses strings. NSURLConnection opens the connection, and it handles the HTTP/1.1 connection reuse for you.
Rob Napier
I need to keep a connection alive and changing NSMutableURLRequest POST-data send different POSTs through it. Is it possible? How?
slatvick
NSURLConnection does this transparently with HTTP/1.1's keep alive and BSD connection reuse, even when you use different NSURLConnection objects (as long as it's just one at a time). You don't need to do anything. Put wireshark on it, and you can see what's going on. Try implementing in the most obvious way and see how the performance is; then look at what needs optimizing, if anything.
Rob Napier
A: 

If I create a connection every time, every time will be sent a header. But I want to send ONLY the body each time (except for the first time, when the connection is created). How do I do it???

Shuriken
A: 

Did you ever figure this out? I've tested with an simple app making NSUrlConnections every 5 sec to my web server, and reading a web page. When I use a sniffer in the web server I see that the iphone is using diffrent port every single time for the http request.

Plato