当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS开发(61)之GCD执行非UI的操作

IOS开发(61)之GCD执行非UI的操作

2019年05月12日  | 移动技术网移动技术  | 我要评论

1 前言

当执行那些与 ui 无关的任务,或者与 ui 交互的任务时,和执行其他任务一样,会需要大量时间,以上情况会经常出现。我们可以使用 dispatch_sync函数在一个分派队列上执行同步任务。你必须做的事情就是提供一个此队列的句柄了,这个队列必须运行任务,并且一个代码块会在这个队列上执行。 今天我们就来学习一下gcd执行非ui的操作。


2 代码实例
testdemo.h

 

[plain]
#import <foundation/foundation.h> 
 
@interface testdemo : nsobject 
 
-(void)testmethod; 
-(void)testmethod2; 
@end 

#import <foundation/foundation.h>

@interface testdemo : nsobject

-(void)testmethod;
-(void)testmethod2;
@end
testdemo.m

 

[plain]
#import "testdemo.h" 
 
@implementation testdemo 
 
/**************objc method start*********/ 
//block object 
void (^printfrom1to1000)(void) = ^{ 
    nsuinteger counter = 0; 
    for (counter = 1;counter <= 1000;counter++){ 
    nslog(@"counter = %lu - thread = %@", 
          (unsigned long)counter, 
          [nsthread currentthread]); 
    } 
}; 
//测试方法 
-(void)testmethod{ 
    /* 
     dispatch_get_global_queue 函数的第一个参数说明了并发队列的优先级,这个属性 gcd 必须替程序员检 
     索。优先级越高,将提供更多的 cpu timeslice 来获取该队列执行的代码。 
     dispatch_get_global_queue 函数的第一个参数:  
     dispatch_queue_priority_low 
     您的任务比正常任务用到更少的 timeslice。  
     dispatch_queue_priority_default 
     执行代码的默认优先级将应用于您的任务。 
     dispatch_queue_priority_high 
     和正常任务相比,更多的 timeslices 会应用到你的任务中。 
     dispatch_get_global_queue 函数的第二个参数已经保存了,只要一直给它输入数值 0 就可以了。 
     */ 
    dispatch_queue_t concurrentqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); 
    dispatch_sync(concurrentqueue, printfrom1to1000); 
    dispatch_sync(concurrentqueue, printfrom1to1000); 

/**************objc method end*********/ 
 
/**************c method start*********/ 
//block object 
void print2from1to1000(void *paramcontext){ 
    nsuinteger counter = 0; for (counter = 1;counter <= 1000;counter++){ 
        nslog(@"counter = %lu - thread = %@", 
        (unsigned long)counter, [nsthread currentthread]); 
    } 

//测试方法 
-(void)testmethod2{ 
    /* 
      
     */ 
    dispatch_queue_t concurrentqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); 
    dispatch_sync_f(concurrentqueue, null,print2from1to1000); 
    dispatch_sync_f(concurrentqueue, null,print2from1to1000); 

/**************c method end*********/ 
@end 

#import "testdemo.h"

@implementation testdemo

/**************objc method start*********/
//block object
void (^printfrom1to1000)(void) = ^{
    nsuinteger counter = 0;
    for (counter = 1;counter <= 1000;counter++){
    nslog(@"counter = %lu - thread = %@",
          (unsigned long)counter,
          [nsthread currentthread]);
    }
};
//测试方法
-(void)testmethod{
    /*
     dispatch_get_global_queue 函数的第一个参数说明了并发队列的优先级,这个属性 gcd 必须替程序员检
     索。优先级越高,将提供更多的 cpu timeslice 来获取该队列执行的代码。
     dispatch_get_global_queue 函数的第一个参数:
     dispatch_queue_priority_low
     您的任务比正常任务用到更少的 timeslice。
     dispatch_queue_priority_default
     执行代码的默认系统优先级将应用于您的任务。
     dispatch_queue_priority_high
     和正常任务相比,更多的 timeslices 会应用到你的任务中。
     dispatch_get_global_queue 函数的第二个参数已经保存了,只要一直给它输入数值 0 就可以了。
     */
    dispatch_queue_t concurrentqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
    dispatch_sync(concurrentqueue, printfrom1to1000);
    dispatch_sync(concurrentqueue, printfrom1to1000);
}
/**************objc method end*********/

/**************c method start*********/
//block object
void print2from1to1000(void *paramcontext){
    nsuinteger counter = 0; for (counter = 1;counter <= 1000;counter++){
        nslog(@"counter = %lu - thread = %@",
        (unsigned long)counter, [nsthread currentthread]);
    }
}
//测试方法
-(void)testmethod2{
    /*
    
     */
    dispatch_queue_t concurrentqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
    dispatch_sync_f(concurrentqueue, null,print2from1to1000);
    dispatch_sync_f(concurrentqueue, null,print2from1to1000);
}
/**************c method end*********/
@end
main.m

 

[plain]
import <foundation/foundation.h> 
#import "testdemo.h" 
 
int main(int argc, const char * argv[]) 

 
    @autoreleasepool { 
         
        testdemo *test = [[testdemo alloc] init]; 
//        [test testmethod]; 
        [test testmethod2]; 
        [test release]; 
         
    } 
    return 0; 

#import <foundation/foundation.h>
#import "testdemo.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        testdemo *test = [[testdemo alloc] init];
//        [test testmethod];
        [test testmethod2];
        [test release];
       
    }
    return 0;
}
运行结果


2013-05-10 13:50:37.361 gcdexecutenonuitest[556:303] counter = 1 - thread = <nsthread: 0x10010b2c0>{name = (null), num = 1}

2013-05-10 13:50:37.363 gcdexecutenonuitest[556:303] counter = 2 - thread = <nsthread: 0x10010b2c0>{name = (null), num = 1}

...

2013-05-10 13:50:38.255 gcdexecutenonuitest[556:303] counter = 1 - thread = <nsthread: 0x10010b2c0>{name = (null), num = 1}

2013-05-10 13:50:38.256 gcdexecutenonuitest[556:303] counter = 2 - thread = <nsthread: 0x10010b2c0>{name = (null), num = 1}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网