如何在一段时间后触发委托方法?
我正在使用一个异步下载文件的类..工作方式有点像这样
// in AViewController.m
DataGetter *blueFile = [[DataGetter alloc] init];
blueFile.delegate = self;
[blueFile getData:@"http://example.com/blue-file"];
它有一个委托方法,可以在下载后对文件执行操作
- (void) dataGetterFinished:(DataGetter *)dataGetter
{
// code
}
这在理想条件下工作正常,但是当我们处理移动设备时,连接并不总是可靠的。连接可能会中途中断,或者速度可能慢得无法使用。
所以我想知道如何设置一个委托方法,该方法在 10 秒后触发,然后显示错误并停止操作。我必须使用 NSTimer 或 NSNotification 或某种组合吗?
I'm using a class which downloads a file asynchronously .. works a bit like this
// in AViewController.m
DataGetter *blueFile = [[DataGetter alloc] init];
blueFile.delegate = self;
[blueFile getData:@"http://example.com/blue-file"];
It has a delegate method which does stuff to the file once downloaded
- (void) dataGetterFinished:(DataGetter *)dataGetter
{
// code
}
This works OK in ideal conditions, but as we're dealing with a mobile device, connections are not always reliable. The connection might break off half way thru, or it might be unusably slow.
So I'm wondering how I would set up a delegate method which triggers after, say, 10 seconds, which then displays an error and stops the operation. Would I have to use NSTimer, or NSNotification , or some combination?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
奎因《爱斯基摩人!》来自 Apple 的在 WWDC 2010 上做了两场关于 iPhone 网络编程的演讲。第 207 场和第 208 场,您可以在这里下载:http://developer.apple.com/videos/wwdc/2010/
网络成功的简单接收方法是:
NSURLConnection
。connection:didFailWithError:
,该连接将因超时而发送。-[NSURLConnection cancel]
手动取消连接,例如响应用户操作。Quinn "The Eskimo!" from Apple did a two talks on network programming for iPhone at WWDC 2010. It's session 207 and 208, you can download them here: http://developer.apple.com/videos/wwdc/2010/
The simple recepie for network success is:
NSURLConnection
asynchronously.connection:didFailWithError:
, that will be sent for time-outs.-[NSURLConnection cancel]
, in response to user action for example.