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];
        }
    });

}

發佈了121 篇原創文章 · 獲贊 7 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章