ios异步中实现按序下载队列

HFSingleonH 头件这里写链接内容

#define HFSingletonH(name) + (instancetype)shared##name;

// .m文件
#if __has_feature(objc_arc)

#define HFSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
}

#else

#define HFSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
} \
\
- (oneway void)release { } \
- (id)retain { return self; } \
- (NSUInteger)retainCount { return 1;} \
- (id)autorelease { return self;}

#endif

头文件


#import <Foundation/Foundation.h>
#include "SingletonMacro.h"
@interface VRImageDownloader : NSObject

HFSingletonH(VRImageDownloader)     //单例

- (void) addDownTasks:(id)item target:(id)tag completed:(void (^)(int result, char *filename, id tag))completedBlock;

@end

实现

#include <pthread.h>

pthread_mutex_t  _downMutex;
typedef void (^completedBlock)(int result, char *filename, id tag);

@interface VRImageDownloader ()
@property (strong, nonatomic) dispatch_queue_t barrierQueue;

@property (strong, nonatomic) NSThread *downThread;

@property (strong, nonatomic) NSMutableArray *arrTask;

@end

@implementation VRImageDownloader

HFSingletonM(VRImageDownloader)

- (instancetype) init{
    self = [super init];
    if(self){
        [self setup];
    }
    return self;
}
- (void) setup{
    pthread_mutex_init(&_downMutex, NULL);
    _barrierQueue = dispatch_queue_create("com.xxx.socketdownQueue", DISPATCH_QUEUE_CONCURRENT);

    _arrTask = [[NSMutableArray alloc] initWithCapacity:10];

    _downThread = [[NSThread alloc] initWithTarget:self
                                          selector:@selector(DownThumbThread)
                                            object:nil];

    [_downThread start];
}
- (void) DownThumbThread{
    [[NSThread currentThread] setName:@"DownThread"];
    __weak __typeof(self)wself = self;
    int downRet;
    while(true){
        //取队列中有没有数据,如果没有,则Sleep
        __block NSDictionary *dic;
        dispatch_barrier_sync(wself.barrierQueue, ^{
            if([wself.arrTask count] >0){
                dic = wself.arrTask[0];
                [wself.arrTask removeObjectAtIndex:0];
            }
        });
        if(dic !=nil){
            //下载
            completedBlock completedBlock ;
            Model *model;
            model = dic[@"item"];

            completedBlock = dic[@"block"];
            XXXManager *cloudManager = [[XXXManager alloc] init];

                pthread_mutex_lock(&_downMutex);  //由于下载是异步的,等待上一次完成,
            [cloudManager requestDown:@"" mediaModel:model progressBlock:^(int progress) {
                dispatch_async_on_main_queue(^{
                    if(completedBlock){
                        completedBlock(progress, (char*)"0", model);
                    }
                });
            } completion:^(Model *sqlModel, NSError *error) {
                dispatch_async_on_main_queue(^{
                    if(completedBlock){
                        completedBlock(downRet, (char*)"", model);
                    }
                        pthread_mutex_unlock(&_downMutex);//上一次完成了
                });
            }];
            usleep(100);   //让线程调度一下,内存不会自动释放下
        }else{
            usleep(10000);
        }
        dic = nil;
    }
}

- (void) addDownTasks:(id)item target:(id)tag completed:(void (^)(int result, char *filename, id tag))completedBlock{

    __weak __typeof(self)wself = self;
    __block int ret=0;

    dispatch_barrier_sync(wself.barrierQueue, ^{
        NSDictionary *dic;
        id obj;
        for(obj in item){
            dic = @{@"tag":tag, @"block":completedBlock,@"item":obj};
            [_arrTask addObject:dic];
        }
    });

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值