且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

-dataWithContentsOfURL:NSData在后台线程中工作吗?

更新时间:2023-02-09 18:40:44

不,它没有。

为了从URL异步获取数据您应该使用 NSURLRequest NSURLConnection 方法。

In order to get data from URL asynchronously you should use the NSURLRequest and NSURLConnection approach.

您必须实施 NSURLConnectionDelegate 方法:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
-(void)connectionDidFinishLoading:(NSURLConnection *)connection;
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;