0

我构建的应用程序有4 tabTabbar Controller),每个选项卡我updateArray在 2 秒后调用一个函数()。我想在单击其他选项卡时,updateArray()功能是杀死。我的问题是什么时候打开tabupdateArray()之后调用2s,当我点击其他选项卡时,这个功能仍然被调用。

这是 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];
   }
}

而在 viewwillappear()

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    loop = YES;
     temp = [FileCompletedArray mutableCopy];
    [self performSelectorInBackground:@selector(updateArray)  withObject:nil ];

}

在我的功能中,我使用[NSThread sleepForTimeInterval:2.0];了 ,我不知道如何杀死它。你有什么建议吗?提前致谢

4

3 回答 3

2

你不应该真正使用sleepForTimeInterval,你应该使用performSelector:withObject:afterDelay:(and cancelPerformSelectorsWithTarget:) 或dispatch_after.

实际上,您可以添加一个 BOOL 属性,用于决定线程是否应该在睡眠后继续,或者是否应该退出(返回)。

于 2013-07-11T10:22:49.387 回答
0

要控制您必须使用的任何线程,您可以NSOperation使用它来控制任何正在运行的线程。

于 2013-07-11T10:25:06.080 回答
0

单击另一个选项卡时创建一个 BOOL,将其设置为 FALSE。在调度之后使用它。

@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. 
 }
});

您应该考虑使用 NSOperation,然后在单击另一个选项卡时可以调用 [NSOperation cancelAllOperations]。

祝你好运,

布朗厄尔

于 2013-07-11T10:31:09.180 回答