iOS GCD吹水

132 阅读2分钟
  • 日常开发中,使用下面两个gcd的api就可以应付大多数场景:
dispatch_async(dispatch_get_main_queue(),^{
//主线程更新UI
});

dispatch_async(dispatch_get_global_queue(0,0),^{
//耗时操作
});
  • 下面是一个例子:
#import<UIKit/UIKit.h>

#import@interface HJTUtility : NSObject

//产生一个速度的随机数

+(int) randomIntBetweenMin:(int) min Max:(int) max;

//生成一个随机的颜色

+(UIColor *)randomColor;

//生成一个随机颜色

+(UIColor *)randomColorWithAlpha:(CGFloat)  alpha;

@end

---------------------------------------------------------------------

#import "HJTUtility.h"

@implementation HJTUtility

+(int)randomIntBetweenMin:(int)min Max:(int)max{

return arc4random() % (max - min + 1) + min;

}

+(UIColor *)randomColorWithAlpha:(CGFloat)alpha{

//利用随机数产生随机颜色

double red = [self randomIntBetweenMin:0 Max:255]/255.0;

double green = [self randomIntBetweenMin:0 Max:255]/255.0;

double blue = [self randomIntBetweenMin:0 Max:255]/255.0;

//alpha  是透明度

return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

}

+(UIColor *)randomColor{

return [self randomColorWithAlpha:1];

}

@end

-------------------------------------------------------------------

建立一个汽车模型   遵循KVC:

#import<UIKit/UIKit.h>

//个人建议使用这个定义字面常量  而不是使用#define  因为我容易弄错 呵呵

static const int CAR_WIDTH = 30;

static const int CAR_HEITH = 40;

@interface HJTCar : NSObject

//汽车左上角横坐标

@property (nonatomic,assign) NSUInteger x;

//汽车左上角纵坐标

@property (nonatomic,assign) NSUInteger y;

//汽车速度

@property (nonatomic,assign) NSUInteger speed;

//和汽车关联的视图

@property (nonatomic,strong) UIView *view;

//初始化汽车左上角的横纵坐标

-(instancetype) initWithX:(int) x Y:(int)y;

//汽车行驶

-(void) carRun;

//将汽车视图呈现在父视图上的方法

-(void) draw:(UIView *)parentView;

@end

---------------------------------------------------------------------

#import "HJTCar.h"

#import "HJTUtility.h"

@implementation HJTCar

-(instancetype)initWithX:(int)x Y:(int)y{

if (self = [super init]) {

_x = x;

_y = y;

_speed = [HJTUtility randomIntBetweenMin:5 Max:9];

_view = [[UIView alloc]initWithFrame:CGRectMake(_x, _y, CAR_WIDTH, CAR_HEITH)];

_view.backgroundColor = [HJTUtility randomColor];

}

return self;

}

-(void)carRun{

while (_y + CAR_HEITH < 650) {

_y += _speed;

CGRect rect = _view.frame;

rect.origin.y = _y;

//苹果官方建议:刷新视图的工作  要回到 主线程完成

dispatch_async(dispatch_get_main_queue(), ^{

_view.frame = rect;

});

//休眠50ms --可以产生20帧(流畅的动画效果)

//区别:sleep()--单位是秒  usleep()--单位是毫秒

usleep(50000);

}

}

-(void)draw:(UIView *)parentView{

[parentView addSubview:_view];

}

@end

--------------------------------------------------------------------

下面是UIViewController中的实现

#import<UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

--------------------------------------------------------------------

#import "ViewController.h"

#import "HJTCar.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

for (int i = 0; i < 5; i++) {

HJTCar *car = [[HJTCar alloc]initWithX:20 + 80 * i Y:10];

[car draw:self.view];

//  把程序中会出现卡顿的地方  放到一个并发队列中执行  避免程序出现卡顿  假死

dispatch_async(dispatch_get_global_queue(0, 0), ^{

[car carRun];

});

}

}

@end
Gcd 常驻线程的危害:

dispatch_async 函数分发到全局队列不一定会新建线程执行任务,全局队列底层有一个的线程池.
如果线程池满了,那么后续的任务会被 block 住,等待前面的任务执行完成,才会继续执行。
如果线程池中的线程长时间不结束,后续堆积的任务会越来越多,此时就会存在 APP crash的风险。