在实现直播送礼物功能的过程中,遇到一种场景
每个gif假设执行时间是1.5秒,那么直播间如果同时有40个人送礼物,
那么观众端的页面1分钟之内都被gif动图占据
动图的播放又不能进行重叠,因此智能同步队列的方式
这种情况下需要舍弃一些用户的礼物gif
另外,对于发礼物的人来说,自己发礼物的gif展示优先级应该比其他人送礼物他接收到的gif要高
异步方案不能用,同步虽然能用,但是没办法插队(插队为了满足用户自己发送礼物的优先展示)
虽然不能够在任务队列中插队,但是可以将任务加进队列的时机进行排队
具体的实现方案核心代码:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) dispatch_queue_t q;
@property(nonatomic,assign)BOOL status;
@property(nonatomic, strong) NSMutableArray * heighArray;
@property(nonatomic, strong) NSMutableArray * lowArray;
@property(nonatomic,strong)UIButton * addLowBtn;
@property(nonatomic,strong)UIButton * addHeightBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.status = NO;
[self start];
// UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
//
// pasteboard.string = @"http://192.168.0.26:8081/#/mobile/live";
self.lowArray = [NSMutableArray arrayWithCapacity:0];
self.heighArray = [NSMutableArray arrayWithCapacity:0];
[self.view addSubview:self.addLowBtn];
[self.view addSubview:self.addHeightBtn];
NSLog(@"---------------原有的lowarray = %@ height array = %@",self.lowArray,self.heighArray);
}
-(void)start{
//队列
self.q = dispatch_queue_create("WT_queue1", DISPATCH_QUEUE_CONCURRENT);
//任务
void (^task)(void) = ^{
self.status = YES;
dispatch_async(self.q, ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 需要延迟执行的代码
self.status = NO;
[self judgeInfoAndStart];
});
});
};
dispatch_sync(self.q, task);
}
-(void)judgeInfoAndStart {
if (!self.status) {
NSLog(@"---------------判断 lowarray = %@ height array = %@",self.lowArray,self.heighArray);
if (self.heighArray.count > 0) {
// self.operaHeight = YES;
[self.heighArray removeObject:self.heighArray.firstObject];
// self.operaHeight = NO;
[self heightTaskInfo];
//根据firstobject来创建任务,加入队列中直接执行
}else {
if (self.lowArray.count > 0) {
[self lowTaskInfo];
// self.operaLow = YES;
//根据firstobject来创建任务,加入队列中直接执行
NSLog(@"origin low array = %@",self.lowArray);
[self.lowArray removeObjectAtIndex:0];
NSLog(@"current low array = %@",self.lowArray);
// self.operaLow = NO;
}
}
}
}
-(void)heightTaskInfo{
//任务
void (^task)(void) = ^{
self.status = YES;
dispatch_async(self.q, ^{
for (int i=0; i< 10; i++) {
NSLog(@"--------------------------新增一个高优先级级任务 %d",i);
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 需要延迟执行的代码
self.status = NO;
[self judgeInfoAndStart];
});
});
};
dispatch_sync(self.q, task);
}
-(void)lowTaskInfo {
//任务
void (^task)(void) = ^{
self.status = YES;
dispatch_async(self.q, ^{
for (int i=0; i< 10; i++) {
NSLog(@"++++++++新增的一个低的优先级任务任务 %d",i);
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 需要延迟执行的代码
self.status = NO;
[self judgeInfoAndStart];
});
});
};
dispatch_sync(self.q, task);
}
-(void)lowAction {
// if(self.operaLow){
// [self lowAction];
// }else {
[self.lowArray addObject:@(1)];
NSLog(@"---------------当前lowarray = %@ height array = %@",self.lowArray,self.heighArray);
if(self.lowArray.count > 10){
[self.lowArray removeObjectsInRange:NSMakeRange(1, 8)];
}
// }
if (!self.status) {
[self judgeInfoAndStart];
}
}
-(UIButton *)addHeightBtn {
if (_addHeightBtn == nil) {
_addHeightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
_addHeightBtn.frame = CGRectMake(50, 280, 100, 50);
[_addHeightBtn setTitle:@"加高" forState:(UIControlStateNormal)];
_addHeightBtn.backgroundColor = [UIColor blueColor];
[_addHeightBtn addTarget:self action:@selector(heightAction) forControlEvents:(UIControlEventTouchUpInside)];
}
return _addHeightBtn;
}
-(void)heightAction {
NSLog(@"点击了抢占队列按钮");
// if (_operaHeight) {
// [self heightAction];
// }else {
[self.heighArray addObject:@(1)];
NSLog(@"---------------当前lowarray = %@ height array = %@",self.lowArray,self.heighArray);
//}
if (!self.status) {
[self judgeInfoAndStart];
}
}
@end
网友评论