简体   繁体   中英

Does all data from URL get loaded into NSData at initialization?

What exactly happens when I do this...?

NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL urlWithString:@"..."]];

Does it immediately go out to the Internet, gets all the data at the URL, returns, and goes to the next line? Or does it set things up, and the data is read later when the bytes of NSData are requested? If the data is read later, is all the data read in one shot? Or is the data read in piecemeal as the app needs them?

My basic problem is that I have a very large XML file to parse. If I set up an NSData object and parse it through NSXMLParser, will the app blow up because the XML data is too large? Or does the app "do the right thing" and parses the XML as the data bytes stream in?

Thanks!

It will immediately go out to the network and fetch that data for you. Your application will block until the request has finished. It is usually not a good idea to do this from the main thread because the user interface will block which makes your application unresponsive.

If you use this method to load your large XML file then the whole file be read into memory and the app will block until that is done.

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