I have a loop function and it calls [NSThread sleepForTimeInterval: 2.0] ;. this means that after 2s the loop function is called. I want this loop function to be stopped on a new view, and when it is back, it is called again.
I use this code to call the loop function:
-(void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; loop = YES; delete=NO; temp = [FileCompletedArray mutableCopy]; NSOperationQueue *queue = [NSOperationQueue new]; operations = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateArray) object:nil]; [queue addOperation:operations]; [operations release]; }
And the loop function:
-(void)updateArray{ while (loop) { NSLog(@"start loop"); if(loop){ [NSThread sleepForTimeInterval:2.0]; NSLog(@"start send request"); NSURL *url1 = [NSURL URLWithString:@"http://server.com"]; NSMutableURLRequest *afRequest = [httpClient requestWithMethod:@"POST" path:nil parameters:params1] ; operation= [[AFHTTPRequestOperation alloc] initWithRequest:afRequest]; NSLog(@" request sent"); [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Server response1"); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@", error); } ]; [httpClient enqueueHTTPRequestOperation:operation]; } else return; } }
And viewdisappear ()
-(void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; loop = NO; delete=NO; [operations cancel] ; }
My problem is to pass in a new view, updateArray still calls. This does not stop. Do you have any suggestions?
source share