iOS开发iOS开发技术学习

iOS多任务断点续传之"框架"封装

2016-05-29  本文已影响1880人  xshenpan

效果演示

demo6.gif

用法和demo:

https://github.com/xshenpan/iOSdemo.git

写它的原因

多任务下载怎么写

typedef void(^XBCompleteBlock)(NSString *filePath, NSError *error);
typedef void(^XBDownloadProgressBlock)(NSInteger bytesRead, NSInteger totalBytesRead , NSInteger totalBytesExpectedToRead);

static const CGFloat kManagerProgressUpdateInterval = 0.5;    //unit : second
static NSString * const kXBDownloadManagerNotification = @"XBDownloadManagerNotification";
static NSString * const kManagerNotificationTaskNumberKey = @"XBDownloadManagerTaskNumber";

@protocol XBDownloadManagerDelegate <NSObject>
@optional
//删除任务时调用,内部保证同一任务的其他代理方法会在该方法之前调用
- (void)managerDeleteTaskForKey:(NSString *)key atIndex:(NSInteger)idx;
//获得响应文件长度是调用
- (void)managerTaskFileLength:(NSInteger)fileLength forKey:(NSString *)key atIndex:(NSInteger)idx;
//进度和速度更新是调用,由kManagerProgressUpdateInterval控制
- (void)managerRefreshTaskProgress:(CGFloat)progress speed:(CGFloat)speed forKey:(NSString *)key atIndex:(NSInteger)idx;
//任务状态改变时调用
- (void)managerTaskStatusChanged:(XBDownloadTaskStatus)status forKey:(NSString *)key atIndex:(NSInteger)idx;
//任务完成时调用
- (void)managerTaskCompleteWithError:(NSError *)error forKey:(NSString *)key atIndex:(NSInteger)idx;
//添加任务,或设置代理时调用
- (void)managerAddTaskName:(NSString *)name andStatus:(XBDownloadTaskStatus)status fileLength:(NSInteger)length forKey:(NSString *)key atIndex:(NSInteger)idx;

@end

@interface XBDownloadManager : NSObject
/** 最大同时下载任务数,最大只能有10个 */
@property (nonatomic, assign) NSInteger maxDownloadTask;
/** 代理 */
@property (nonatomic, weak, readonly) id<XBDownloadManagerDelegate> delegate;
/** 任务数量 */
@property (nonatomic, assign, readonly) NSInteger taskNumber;


//控制方法
- (void)startAllDownloadTask;
- (void)pauseAllDownloadTask;
- (void)startWithIndex:(NSInteger)idx;
- (void)pauseWithIndex:(NSInteger)idx;
- (void)cancelWithIndex:(NSInteger)idx;
- (void)startWithKey:(NSString *)key;
- (void)pauseWithKey:(NSString *)key;
- (void)cancelWithKey:(NSString *)key;
- (void)reloadWithKey:(NSString *)key;
//查询任务
- (XBDownloadTaskInfo *)taskInfoWithIndex:(NSInteger)idx;
- (XBDownloadTaskInfo *)taskInfoWithKey:(NSString *)key;
//设置代理和代理执行的队列
- (void)setDelegate:(id<XBDownloadManagerDelegate>)delegate andDelegateQueue:(NSOperationQueue *)queue;
/** 添加一个下载任务,path=nil这默认在cache/xbdownload目录 路经以home开始, key=nil 则key = url.md5string */
- (NSString *)addDownloadTaskWithUrl:(NSString *)url andRelativePath:(NSString *)path taskKey:(NSString *)key taskExist:(void(^)(NSString *key))exist;

+ (instancetype)manager;

管理类怎么实现?

//三个主要的调度方法
//尝试启动指定的任务
- (void)tryStartupTheTask:(XBDownloadTaskRecord *)taskRecord;
//尝试启动等待的任务
- (NSInteger)tryStartupWaitingTask;
//根据任务记录,创建下载任务
- (void)startupTaskWithRecord:(XBDownloadTaskRecord *)taskRecord;

最后

补充一点

上一篇下一篇

猜你喜欢

热点阅读