在 iPhone 上刷新数据会导致系统变得不稳定或无响应

发布于 2024-09-24 21:45:39 字数 1650 浏览 3 评论 0原文

大家我在这里遇到问题

我需要在一段时间内更新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮倦 2024-10-01 21:45:39

如果您使用异步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

清晰传感 2024-10-01 21:45:39

不要使用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 NSURLConnections only. You can send them a cancel message at any time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文