iOS 多线程 NSInvocationOperation

 //以前一直不知道多线程是个什么东西,为什么要用到线程。直到项目里遇到请求XML时。当数据加载或刷新时界面就会卡。也就是主线程堵塞。后来终于把这个问题给解决了。
 //初始化
  _operationQueue = [[NSOperationQueue alloc]init];
//设置每秒请求一次 。在这里selector:@selector(threadTestN)后面注意如果threadTestN没有带冒号,表示这个方法不带参数。如果带上了冒号":"表示这个方法是有参数的
   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1    
                                                      target:self
                                                    selector:@selector(threadTestN)
                                                    userInfo:nil
                                                     repeats:YES];

-(void)threadTestN
{
   //启动线程。加入队列
    [_operationQueue addOperation:[self startThread]];
}


-(NSInvocationOperation*)startThread
{
    NSInvocationOperation *requestThread = [[NSInvocationOperation alloc]initWithTarget:self
                                                                               selector:@selector(ThreadTestMeth) 
                                                                                 object:nil];
    
    
    return [requestThread autorelease];
}

-(void)ThreadTestMeth 
{
     //这里开始加载从XML请求来的数据
     [threadLoad loginNoticeDate];
   
}

//这样就基本每秒从服务器请求一次 。再也不用担心主线程会阻塞了。

qingjoin 

原文地址:https://www.cnblogs.com/qingjoin/p/2665139.html