放到 子线程里面,就不卡了,

-(void)doInitialWork

{

    mStream = [[XMPPStreamalloc]init];

    dispatch_queue_t im_queue;

    im_queue = dispatch_queue_create("im.queue", Nil);

    [mStreamaddDelegate:selfdelegateQueue:im_queue];

    dispatch_block_t b = ^{

        TTLog(@"aa");

    };

同步,不同步,是任务安装添加的顺序执行而已,还是顺序执行的,同步的话,第一个执行完了,第二个才执行,而不同步的话,两个不是安装顺序来执行的,

队列是队列,任务(block)是任务,执行是执行,跳转是跳转,

    dispatch_async(im_queue, b);

    dispatch_sync(im_queue, b);

}

一个特点:要在主线程中 操作ui,

1,

dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 处理耗时操作的代码块...

//通知主线程刷新
dispatch_async(dispatch_get_main_queue(), ^{
//回调或者说是通知主线程刷新,
});

});

2,

-(void)setupThread:(NSArray*)userInfor{

[NSThread detachNewThreadSelector:@selector(threadFunc:) toTarget:self withObject:(id)userInfor];

}

- (void)threadFunc:(id)userInfor{

NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];

//。。。。需要做的处理。

//这里线程结束后立即返回

[self performSelectorOnMainThread:@selector(endThread) withObject:nil waitUntilDone:NO];

[pool release];

}

dispatch_async() 调用以后立即返回,dispatch_sync() 调用以后等到block执行完以后才返回,dispatch_sync()会阻塞当前线程

子线程内不要进行任何UI操作,不要刷新界面。如果需要进行这些操作,通过dispatch_async或performSelectorOnMainThread这两种方法,调出主线程中的方法去执行。

原文地址:https://www.cnblogs.com/guligei/p/3511824.html