0

我是一名 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

}
4

1 回答 1

0

你有理由选择 NSThread 而不是 NSOperation 吗?NSOperation 抽象了很多你在使用 NSThread 时不得不担心的低级问题。我强烈建议您阅读 Apple 的这个并发编程指南

请注意最后一部分,从线程迁移,因为它讨论了可以帮助您编写健壮、干净的代码的其他替代方案。

于 2012-07-24T03:57:01.803 回答