views:

785

answers:

2

Hi

I want to get an NSData object's contents from a URL. What is the more efficient way of doing this in terms of memory usage dataWithContentsOfURL (or initWithContentsOfURL) or using NSURLConnection?

Should I use

NSData *data = [[NSData alloc] initWithContentsOfURL:myURL]

or

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
A: 

Pretty sure they are quite equivalent.

Squeegy
+2  A: 

I don't know the internals of Apple's code but I would guess NSData's initWithContents of URL uses NSURLConnection internally. Memory usage differences will be negligible.

Using the asynchronous apis of NSURLConnection would allow you to be more memory efficient by handling data as it came in but (without knowing what you are actually doing) I think this is a fairly agressive optimisation that you should leave until you have working code.

Roger Nolan