开发者

How to cancel the link like this?

开发者 https://www.devze.com 2023-03-21 17:46 出处:网络
I used a NSTimer to get the update data in a link every seconds,and the method like this: NSString *url=[NSString stringWithFormat:@\"http://disca.mal开发者_如何学Cauu.com/jsbfAll.txt\"];

I used a NSTimer to get the update data in a link every seconds,and the method like this:

NSString *url=[NSString stringWithFormat:@"http://disca.mal开发者_如何学Cauu.com/jsbfAll.txt"];
NSError *error=[NSString stringWithFormat:@"You are error"];
NSString *urlData=[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&error];
NSLog(@"urlString:%@",urlData);

And if the network conges,the connection will connect for a long time.How can i cancel it if the link doesn't get the update data with half of second? Who can get me a answer,Think you very much!


If think you could implement it using NSURLConnection and delegate pattern which allow you cancel the connection, and set options like a timeout.
The second advantage is that the request is performed asynchronously, and won't block the execution of your app, if the network doesn't answer immediatly.

You will find more information about this way, on NSURLConnection Class Reference page, and in URL Loading System Programming Guide.

0

精彩评论

暂无评论...
验证码 换一张
取 消