views:

27

answers:

2

Hi ,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

+2  A: 

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.

Ole Begemann
Thanks for your reply,What if I don't want to cancel the refresh action,just terminated when I switch to another view ?and how to use NSURLConnection to keep refresh the data ?
WebberLai
+2  A: 

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

Yannick Compernol
OK...you means no matter what I need to stop the timer when the view is not shown,so do I need to remove the code not in ViewDidLoad ?or give another void to update my data every two seconds ???
WebberLai
You could start the timer in viewDidAppear and stop it in viewDidDisappear.
Yannick Compernol
I'll try it !and tell you the result
WebberLai
YES,your solution is correct !
WebberLai