I building an app has 4 tab
(Tabbar Controller
), and each tab I call a function (updateArray
) after 2s. I want to when click on other tab, updateArray()
function is kill. My problem is when on tab
, updateArray()
call after 2s
, when I click on other tab, this function is still call.
This is updateArray()
-(void)updateArray{
while (loop)
{
[NSThread sleepForTimeInterval:2.0];
[FileCompletedArray removeAllObjects];
[temp removeAllObjects];
[UserNameArray removeAllObjects];
NSURL *url1 = [NSURL URLWithString:@"server"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: url1] ;
NSMutableURLRequest *afRequest = [httpClient requestWithMethod:@"POST" path:nil parameters:params1] ;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success");
NSString * parsexmlinput = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response in Loop CompleteView: %@", parsexmlinput); //000400010001
// dispatch_async(dispatch_get_main_queue(), ^{
[self parseXMLFile:parsexmlinput];
NSLog(@"File Completed array: %@", FileCompletedArray);
NSLog(@"File Temp out array: %@", temp);
NSLog(@"File Completed count: %lu",(unsigned long)[ FileCompletedArray count]);
NSLog(@"File Temp out count: %lu", (unsigned long)[temp count]);
if([FileCompletedArray count] != [temp count])
{
temp = [FileCompletedArray mutableCopy];
NSLog(@"File Temp 1 array: %@", temp);
[_tableView reloadData];
NSLog(@"File Temp 2 array: %@", temp);
}
[alert dismissWithClickedButtonIndex:0 animated:YES];
//});
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", error);
}
];
[httpClient enqueueHTTPRequestOperation:operation];
}
}
And in viewwillappear()
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
loop = YES;
temp = [FileCompletedArray mutableCopy];
[self performSelectorInBackground:@selector(updateArray) withObject:nil ];
}
In my function, i used [NSThread sleepForTimeInterval:2.0];
, I don't know how to kill it. Do you have suggestions ? Thanks in advance