IOS开发之多线程(转)

本文详细介绍了iOS开发中常见的多线程技术,包括使用self performSelectorInBackground、NSThread、NSOperationQueue及GCD等方法实现后台任务处理与UI更新的具体操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序中需要有并行的操作,就会用到多线程

1、

[self performSelectorInBackground:@selector(task) withObject:nil]; 


2、NSThread代表执行的线程,可以使用该类的对象封装线程操作,可以使用该类创建、管理线程


   a、使用类方法 detachNewThreadSelector: toTarget: withObject:创建新的线程

[NSThread detachNewThreadSelector:@selector(task) toTarget:self withObject:nil];


   b、创建NSTheard对象。

    NSThread *thr = [[NSThread alloc]initWithTarget:self selector:@selector(task) object:nil];

    [thr start];


   c、自定义Thread对象
    @interface myThread:NSThread
    @implementation myThread
    -(void)main
     {
       //在此添加线程方法
     }
     myThread *_thread = [[myThread alloc]init];
     [myThread start]; 


3、NSOperationQueue:作为Operation的管理者,OperationQueue负责执行放入其中的所有操作对象

- (IBAction)Caculate:(id)sender 
{
                 
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue setMaxConcurrentOperationCount:1];//指定能够同时并发执行的操作个数
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(xiazai1) object:nil];
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(xiazai2) object:nil];
    NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(xiazai3) object:nil];
    [queue addOperation:op1];//将创建好的operation加入运行队列中
    [queue addOperation:op2];
    [queue addOperation:op3];
        
    
}
//从网上下载图片
-(void)xiazai1
{
    NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.qqskycn.net/bq/bq_tp/200904/20090404193057343.gif"]];
    NSLog(@"data1 = %@",data1);
    Image1.image = [UIImage imageWithData:data1];

}
-(void)xiazai2
{
    NSData *data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://imgqq.5308.com/20061107/uploadfile/2005/8/11/14511674586.jpg"]];
    Image2.image = [UIImage imageWithData:data2];

    
}
-(void)xiazai3
{
    NSData *data3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img1.comic.zongheng.com/comic/image/2009/1/samosekayi/ori/20090217025931686184.jpg"]];
    Image3.image = [UIImage imageWithData:data3];

    
}


4、

GCD是和block紧密相连的,所以最好先了解下block(可以查看这里).GCD是C level的函数,这意味着它也提供了C的函数指针作为参数,方便了C程序员.

下面首先来看GCD的使用:

dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

async表明异步运行,block代表的是你要做的事情,queue则是你把任务交给谁来处理了.(除了async,还有sync,delay,本文以async为例).

之所以程序中会用到多线程是因为程序往往会需要读取数据,然后更新UI.为了良好的用户体验,读取数据的操作会倾向于在后台运行,这样以避免阻塞主线程.GCD里就有三种queue来处理.

1. Main queue:

  顾名思义,运行在主线程,由dispatch_get_main_queue获得.和ui相关的就要使用Main Queue.

2.Serial quque(private dispatch queue)

  每次运行一个任务,可以添加多个,执行次序FIFO. 通常是指程序员生成的,比如:

NSDate *da = [NSDate date];
NSString *daStr = [da description];
const char *queueName = [daStr UTF8String];
dispatch_queue_t myQueue = dispatch_queue_create(queueName, NULL);

3. Concurrent queue(global dispatch queue):

可以同时运行多个任务,每个任务的启动时间是按照加入queue的顺序,结束的顺序依赖各自的任务.使用dispatch_get_global_queue获得.

所以我们可以大致了解使用GCD的框架:

复制代码
dispatch_async(getDataQueue,^{
    //获取数据,获得一组后,刷新UI.
    dispatch_aysnc (mainQueue, ^{
    	//UI的更新需在主线程中进行
    });
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值