0

I'm an iPhone developer. I studied about how to use NSThread. So I created source code. But, I'm not sure about my source code whether good or bad.

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

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

Sometimes, threadCount doesn't become 4. So, ActiveView is worked continual without stopping. Turn the timer after a period of time, remove ActiveView? I'll give you some advice please.

-(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

}
hyekyung
  • 671
  • 2
  • 14
  • 27
  • 2
    This thread is utterly pointless, why did you make it? All it does it run stuff on the main thread.... – borrrden Jul 24 '12 at 03:41
  • The queue you creat I'm not sure but it seems you are not using it. You are creative it then the nsthrad but you are thinking of creating 4 NSOperations? NSthread is something you need a very long-reapeating-conditional based stuff that needs to be done like a game drawing loop or a chat application. Things you will need to keep a constant eye on. – Pareshkumar Jul 24 '12 at 03:50

1 Answers1

0

Do you have a reason as to why you are opting for NSThread over NSOperation? NSOperation abstracts away a lot of the lower-level thing you would have to worry about with NSThread. I would strongly recommend you read up on this concurrency programming guide from Apple.

Pay attention to the last section, Migrating Away From Threads, as it talks about additional alternatives that can help you write robust, clean code.

Stunner
  • 12,025
  • 12
  • 86
  • 145