lostInTransit
lostInTransit

Reputation: 70997

NSData - Which is better in terms of memory usage: initWithContentsOfURL or NSURLConnection

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];

Upvotes: 1

Views: 1372

Answers (2)

Rog
Rog

Reputation: 17170

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.

Upvotes: 2

Alex Wayne
Alex Wayne

Reputation: 187064

Pretty sure they are quite equivalent.

Upvotes: 0

Related Questions