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 
#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_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)