0

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

Paolo Stefan
  • 10,112
  • 5
  • 45
  • 64
NGOT
  • 149
  • 5
  • 12

3 Answers3

2

You shouldn't really use sleepForTimeInterval, you should use performSelector:withObject:afterDelay: (and cancelPerformSelectorsWithTarget:) or dispatch_after.

As it is, you can add a BOOL attribute that is used to decide if the thread should continue after the sleep or whether it should exit (return).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Yes, I used loop variable (BOOL type), and put it at first updateArray funtion. but it still run when i click on other tab. – NGOT Jul 11 '13 at 10:30
  • So after `[NSThread sleepForTimeInterval:2.0];` you should add `if (!loop) return;` – Wain Jul 11 '13 at 10:34
  • No need to add a bool; just call `cancel` on the `NSThread` instance, then when the Timer fires (yes, use a Timer), test `[[NSThread currentThread] isCancelled]` to see it if returns YES. – bbum Jul 11 '13 at 13:04
  • @bbum: as you said, i would like create NSThread instance on init and use it to cancel??? – NGOT Jul 12 '13 at 03:15
0

To control any thread you have to use NSOperation Using this you can control any running thread.

Divyam shukla
  • 2,038
  • 14
  • 18
0

Create a BOOL when you click on another tab set it to FALSE. Use this with dispatch after.

@property (nonatomic, assign) BOOL doUpdate;

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

if(doUpdate){
   //update work. 
 }
});

You should take look at using NSOperation and then you could call [NSOperation cancelAllOperations] when clicking on a another tab.

Good luck,

Booranger

BooRanger
  • 1,516
  • 1
  • 14
  • 18
  • Sorry couldn't be of help, might be worth messing about with canceling your network operations, [[[httpClient sharedClient] operationQueue] cancelAllOperations]; – BooRanger Jul 11 '13 at 10:54