开发者

Refreshing data on iPhone causes the system to become unstable or unresponsive

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/e开发者_开发知识库nvotouch/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


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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜