ios - Since we have [NSData dataWithContentsOfURL:], why do we use [NSURLConnection sendSynchronousRequest:returningResponse:error:]? -
we can fetch simple web contents codes below:
+ (nsstring *)getcontentwithurl:(nsstring *)urlstring { nsurl *url = [nsurl urlwithstring:urlstring]; nsdata *data = [nsdata datawithcontentsofurl:url]; if(!data) { homecoming @""; } else { homecoming [nsstring stringwithutf8string:[data bytes]]; } }
but told me using nsurlconnection?
both different things:
datawithcontentsofurl:
this method ideal converting data:// urls nsdata objects, , can used reading short files synchronously. if need read potentially big files, utilize inputstreamwithurl: open stream, read file piece @ time.
important: not utilize synchronous method request network-based urls. network-based urls, method can block current thread tens of seconds on slow network, resulting in poor user experience, , in ios, may cause app terminated.
sendsynchronousrequest:returningresponse:error:
a synchronous load built on top of asynchronous loading code made available class. calling thread blocked while asynchronous loading scheme performs url load on thread spawned load request. no special threading or run loop configuration necessary in calling thread in order perform synchronous load.
important: because phone call can potentially take several minutes fail (particularly when using cellular network in ios), should never phone call function main thread of gui application.
ios objective-c nsurlconnection
No comments:
Post a Comment