您的位置:首页 > 移动开发 > IOS开发

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

2017-09-01 14:59 423 查看
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];
}
});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 异步 队列