iOS开发-在视频直播媒体中 礼物发送 NSOperation使用

iOS开发-在视频直播媒体中 礼物发送 NSOperation使用

前言

  • iOS开发中需要使用到NSOperation进行对礼物发送的操作,例如在视频直播中。

效果

在这里插入图片描述

开发前准备

  • pod导入
    pod 'SDWebImage'
    pod 'YYModel'

关键代码

  • LDSGiftOperation.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class LDSGiftModel,LDSGiftShowView;
typedef void(^completeOpBlock)(BOOL finished,NSString *giftKey);
@interface LDSGiftOperation : NSOperation

/**
 增加一个操作

 @param giftShowView 礼物显示的View
 @param backView 礼物要显示在的父view
 @param model 礼物的数据
 @param completeBlock 回调操作结束
 @return 操作
 */
+ (instancetype)addOperationWithView:(LDSGiftShowView *)giftShowView
                              OnView:(UIView *)backView
                                Info:(LDSGiftModel *)model
                       completeBlock:(completeOpBlock)completeBlock;


/** 礼物展示的父view */
@property(nonatomic,strong) UIView *backView;
/** model */
@property(nonatomic,strong) LDSGiftModel *model;

/** block */
@property(nonatomic,copy) completeOpBlock opFinishedBlock;
/** showview */
@property(nonatomic,strong) LDSGiftShowView *giftShowView;

@end

  • LDSGiftOperation.m
#import "LDSGiftOperation.h"
#import "LDSGiftShowView.h"
#import "LDSGiftModel.h"

@implementation LDSGiftOperation

@synthesize finished = _finished;
@synthesize executing = _executing;

+ (instancetype)addOperationWithView:(LDSGiftShowView *)giftShowView
                              OnView:(UIView *)backView
                                Info:(LDSGiftModel *)model
                       completeBlock:(completeOpBlock)completeBlock {
    
    
    LDSGiftOperation *op = [[LDSGiftOperation alloc] init];
    op.giftShowView = giftShowView;
    op.model = model;
    op.backView = backView;
    op.opFinishedBlock = completeBlock;
    return op;
}

- (instancetype)init {
    
    
    if (self = [super init]) {
    
    
        _executing = NO;
        _finished  = NO;
    }
    return self;
}

- (void)start {
    
     //子类要实现的方法
    if ([self isCancelled]) {
    
    
        _finished = YES;
        return;
    }
    
    _executing = YES;
    //NSLog(@"当前队列-- %@",self.model.giftName);
    __weak typeof(self) weakSelf = self;
    //NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    
    
        NSLog(@"currentThread - %@", [NSThread currentThread]);
        [weakSelf.backView addSubview:weakSelf.giftShowView];
        
        [weakSelf.giftShowView showGiftShowViewWithModel:weakSelf.model completeBlock:^(BOOL finished,NSString *giftKey) {
    
    
            weakSelf.finished = finished;
            if (weakSelf.opFinishedBlock) {
    
    
                weakSelf.opFinishedBlock(finished,giftKey);
            }
        }];
    }];
    
}

#pragma mark - 手动触发 KVO

- (void)setExecuting:(BOOL)executing {
    
    
    [self willChangeValueForKey:@"isExecuting"];
    _executing = executing;
    [self didChangeValueForKey:@"isExecuting"];
}

- (void)setFinished:(BOOL)finished {
    
    
    [self willChangeValueForKey:@"isFinished"];
    _finished = finished;
    [self didChangeValueForKey:@"isFinished"];
}

@end

Demo

猜你喜欢

转载自blog.csdn.net/weixin_41732253/article/details/110279259