在 iPhone 上刷新数据会导致系统变得不稳定或无响应
大家我在这里遇到问题
我需要在一段时间内更新plist数据
,我使用Tab Bar切换2个视图
当我选择view1时,它将从URL加载数据
但是如果我切换到view2,view1仍然更新data
如果切换到view2并切换回来,view2会不断更新数据。
这是我用来更新数据的代码 在 LoadData.h
@interface LoadData : UITableViewController < NSNetServiceBrowserDelegate > {
NSArray *plist;
NSTimer *timer;
}
在 LoadData.m
static const float REFRESH_STATUS_TIME = 2.0;
- (void)viewDidLoad {
timer = [NSTimer scheduledTimerWithTimeInterval:REFRESH_STATUS_TIME
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:YES];
[super viewDidLoad];
}
- (void)timerFired:(NSTimer *)theTimer{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://10.85.28.99/envotouch/req_light.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSLog(@"\n\nCONNECTION: %@", theConnection);
NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
self.plist = [listFile propertyList];
[self.tableView reloadData]
}
所以我的问题是当我切换到另一个视图时如何终止数据更新?
感谢您的回复......这是一个让我烦恼的大错误
everyone I got problem here
I need to update a plist data in a period time
and I use Tab Bar to switch 2 views
When I select to view1 ,It will load data from an URL
But if I switch to view2 , the view1 still update the data
If you switch to view2 and switch back ,view2 keep updating the data.
and this is the code I'm using to update the data
in LoadData.h
@interface LoadData : UITableViewController < NSNetServiceBrowserDelegate > {
NSArray *plist;
NSTimer *timer;
}
in LoadData.m
static const float REFRESH_STATUS_TIME = 2.0;
- (void)viewDidLoad {
timer = [NSTimer scheduledTimerWithTimeInterval:REFRESH_STATUS_TIME
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:YES];
[super viewDidLoad];
}
- (void)timerFired:(NSTimer *)theTimer{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://10.85.28.99/envotouch/req_light.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
NSLog(@"\n\nCONNECTION: %@", theConnection);
NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
self.plist = [listFile propertyList];
[self.tableView reloadData]
}
So my question is how to terminated the data update when I switch to another view ?
thanks for the reply.....this a big bug annoying me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用异步
NSURLConnection
,您可以在切换到另一个视图时向连接(仍忙于加载)发送取消消息。异步方法将使您的 UI 保持响应,这是一个额外的好处,而同步方法则不然,正如 Ole Begemann 指出的那样。您可以在此处找到有关如何使用异步方法的信息: URL 加载系统编程指南
编辑: 您可能还应该停止计时器(通过向其发送无效消息)视图未显示,这样当视图未显示导致数据加载时,计时器不会触发。
NSTimer 类参考
If you use the asynchronous
NSURLConnection
you can send the cancel message to the connection (that is still busy loading) when you switch to another view. The asynchronous method will keep your UI responsive as a bonus, which the synchronous method does not as Ole Begemann pointed out.You can find information on how to use the asynchronous method here: URL Loading System Programming Guide
Edit: You should presumably also stop the timer (by sending it an invalidate message) when the view is not shown, this way the timer does not fire when the view is not shown causing the load of data.
NSTimer Class Reference
不要使用
sendSynchronousRequest:returningResponse:error:
。您的程序在该调用期间完全被阻止,如果网络无响应或服务器关闭,甚至可能被操作系统终止。仅使用异步
NSURLConnection
。您可以随时向他们发送取消
消息。Do not use
sendSynchronousRequest:returningResponse:error:
. Your program is completely blocked during that call and can even be terminated by the OS if the network is unresponsive or the server is down.Use asynchronous
NSURLConnection
s only. You can send them acancel
message at any time.