开发者

iphone network multithreading

I have a class for receiving data from network,named NetWorkConnect.the following method is in NetWorkConnect.m,this is delegate method. m_DisplayMarketViewController is an instance of class DisplayMarketViewController.

-(void)stream:(NSStream*)st开发者_如何学Cream handleEvent:(NSStreamEvent)eventCode{
    switch (eventCode) {
        case NSStreamEventHasBytesAvailable:
        {
            if (data == nil) {
            data = [[NSMutableData alloc] init];
            }
            uint8_t buf[1024];
            unsigned int len = 0;
            len = [(NSInputStream *)stream read:buf maxLength:1024];
            if(len) {    
            [data appendBytes:(const void *)buf length:len];
            } else {
            NSLog(@"No data.");
            }  
           [self storeData:data];              
           [m_DisplayMarketViewController updateMarket:self];                   
    } break;  
    default:
      break;
    }
}

the method updateMarket passes the self to DisplayMarketViewController,so DisplayMarketViewController can use the data which received from network.and in DisplayMarketViewController.m file the data will be displayed.but when I update the data,i can display the data ,but the inteface seems a little blocked,so I intend to use multiThreading....but how to do it?thank you.


You can subclassing  NSOperation and in implementation file you can try something like this.

- (void)main 
{
    @try 
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        executing_ = YES;
       [self performSelector:@selector(makeSomethingInBackground:) onThread:[NSThread      currentThread] withObject:nil waitUntilDone:NO];
[pool drain];
    }
    @catch (NSException * e)  {
        NSLog(@"Exception: %@", e);
    }
}


#pragma mark -
#pragma mark Overrides

- (BOOL)isConcurrent
{
    return YES;
}

- (BOOL)isExecuting
{
    return executing_;
}

- (BOOL)isFinished
{
    return finished_;
}

- (void)cancel
{
    [super cancel];
    [self done];
}
In another class create instance of your NSOperation subclass.
YourNSOperationSubclass *operation = [YourNSOperationSubclass new];
NSOperationQueue *op = [NSOperationQueue new];  [op addOperation operation];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜