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 利用 ASI 实现的异步队列下载文件
- IOS---利用ASIHTTPRequest实现异步队列
- iOS学习系列 - 利用ASIHTTPRequest实现异步队列
- ios异步登录,登录的是php写服务器,同理可以实现文件图片下载
- iOS学习笔记---利用NSThread实现异步更新UI和下载图片
- iOS实现NSURLConnection进行异步下载
- 基于iOS 使用GCD实现列表界面的图片异步下载
- IOS可以实现异步下载但是无网条件下不行
- iOS-实现文件上传下载
- IOS 下载文件断点续传原理与实现(附源码)
- iOS开发之结合asp.net webservice实现文件上传下载
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等(有示例代码)
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
- iOS学习笔记-106.多线程05——CGD同步、异步函数和并行、串行、主队列示例
- ios开发网络学习十:利用文件句柄实现大文件下载
- iOS:操作队列实现多线程NSOperation
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别
- Microsoft .NET Framework 2.0对文件传输协议(FTP)操作(异步上传,下载等)实现汇总2
- 利用 Remoting 实现异步队列机制
- 用Java实现HTTP文件队列下载