【问题标题】:About how to use NSThread关于如何使用 NSThread
【发布时间】:2012-07-22 08:27:00
【问题描述】:

我是一名 iPhone 开发人员。 我研究了如何使用 NSThread。所以我创建了源代码。 但是,我不确定我的源代码是好是坏。

 -(void)check_threadEnd 
    {
           if ([_thread isFinished]) {
            threadCount++; 

            if (threadCount == 4) {
                [self performSelector:@selector(removeActivityView) withObject:nil afterDelay:0.0];                 
                [self.tableView reloadData];               
            }         
        }
    }

有时,threadCount 不会变为 4。 因此,ActiveView 不间断地连续工作。 一段时间后打开定时器,去掉ActiveView? 请给你一些建议。

-(IBAction)click_ServerSync:(id)sender
{
    if ([util checkNetwork]) {
        threadCount = 0 ;         
        [self displayActivityView]; 

        NSOperationQueue  *queue = [[[NSOperationQueue alloc] init] autorelease];
       [queue setMaxConcurrentOperationCount:4]; 
        _thread = [[NSThread alloc] initWithTarget:self selector:@selector(_th) object:nil];
        [_thread start];       
    } 
}

-(void)_th
{
  [self performSelectorOnMainThread:@selector(LoadXml:) withObject:@"XML1"   waitUntilDone:NO];
  [self performSelectorOnMainThread:@selector(LoadXml:) withObject:@"XML2"   waitUntilDone:NO];
  [self performSelectorOnMainThread:@selector(LoadXml:) withObject:@"XML3"   waitUntilDone:NO];
  [self performSelectorOnMainThread:@selector(LoadXml:) withObject:@"XML4"   waitUntilDone:NO]; 
}

-(void)LoadXml:(NSString*)P_VAL
{     
    NSString *smsURL = [NSString stringWithFormat:@"%@%@.asp", XML_URL, P_VAL];   

    NSString *sendAuthInfo = [NSString stringWithFormat:@"A=%@&B=%d&C=%@&D=%@"  , A, B, C, D ];   
    NSString *val = [sendAuthInfo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:smsURL]]autorelease];

    [request setURL:[NSURL URLWithString:smsURL]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: [val dataUsingEncoding: NSUTF8StringEncoding]];    
    [self startAsyncLoad:request tag:P_VAL];   
}


- (void)startAsyncLoad:(NSMutableURLRequest*)request tag:(NSString*)tag {

    CustomURLConnection *connection = [[CustomURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES tag:tag];

    if (connection) { 
        [receivedData setObject:[[NSMutableData data] retain] forKey:connection.tag];
    }    
}

- (NSMutableData*)dataForConnection:(CustomURLConnection*)connection {
    NSMutableData *data = [receivedData objectForKey:connection.tag];
    return data;
}

-(void)check_threadEnd 
{
       if ([_thread isFinished]) {
        threadCount++; 

        if (threadCount == 4) {
            [self performSelector:@selector(removeActivityView) withObject:nil afterDelay:0.0];                 
            [self.tableView reloadData];               
        }         
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {    
    NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
    [dataForConnection setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {   
    NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
    [dataForConnection appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{     
    NSLog(@" connection connectionDidFinishLoading : %d", [connection retainCount]);    
    NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection];
    [connection release];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;    
    NSXMLParser *xmlParser = [[[NSXMLParser alloc] initWithData:dataForConnection] autorelease];  
    XMLParser *parser = [[XMLParser alloc] initXMLParser];      
    [xmlParser setDelegate:(id)parser];    
    parser.viewDelegate = (id)self; 
    [xmlParser parse];   // xml parser

}

【问题讨论】:

  • 这个帖子完全没有意义,你为什么要发这个帖子?它所做的一切都是在主线程上运行的......
  • 您创建的队列我不确定,但您似乎没有使用它。你在 nsthrad 之前很有创意,但你正在考虑创建 4 个 NSOperations? NSthread 是你需要一个非常长的重复条件的东西,需要像游戏绘图循环或聊天应用程序一样完成。您需要时刻关注的事情。

标签: iphone ios xml-parsing nsthread


【解决方案1】:

您是否有理由选择 NSThread 而不是 NSOperation? NSOperation 抽象了很多你在使用 NSThread 时不得不担心的低级问题。我强烈建议您阅读来自 Apple 的 concurrency programming guide

请注意最后一节 Migrating Away From Threads,因为它讨论了可以帮助您编写健壮、干净的代码的其他替代方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多