RicDownloadTask *downloadTask = [[RicDownloadTask alloc] init];
NSDictionary *taskInfo = @{@"downloadUrl":@"http://a4.att.hudong.com/38/47/19300001391844134804474917734_950.png"};
downloadTask.customInfomation = taskInfo;
downloadTask.progressHandle = ^(CGFloat progress){ NSLog(@"progress :%f",progress); };
downloadTask.downloadAction = ^(RicTask* task,CompeletedNotice notice){ NSLog(@"下载开始";
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString: task.customInfomation[@"downloadUrl"]]; NSURLSessionDownloadTask *dataTask = [defaultSession downloadTaskWithURL: url];
[dataTask resume]; };
RicTaskHelper *helper = [[RicTaskHelper alloc] init]; [helper addTask:downloadTask];
[helper startTasks:^{
} progressHandle:^(NSInteger compeletedCount, NSInteger totalCount) { NSLog(@"%ld/%ld",compeletedCount,totalCount);
} compeleteAction:^{ NSLog(@"完成"); }];
// 准备要上传的资源或资源信息 RicUploadTask *uploadTask = [[RicUploadTask alloc] init];
uploadTask.customInfomation = @{@"uploadData":@"some data here"};
uploadTask.uploadAction = ^(RicTask task,CompeletedNotice noticeBlock){ // 从task.customInfomation获取数据
/// 在这里进行上传操作。
// 当完成时
noticeBlock();
};
RicTaskHelper *uploadHelper = [[RicTaskHelper alloc] init];
[uploadHelper addTask:uploadTask];
[uploadHelper startTasks:^{
// 执行UI加载
} progressHandle:^(NSInteger compeletedCount, NSInteger totalCount) {
} compeleteAction:^{
}];
要运行示例项目,请克隆仓库,并首先从Example目录运行pod install
。
RicTaskHelper可通过CocoaPods获得。要安装它,只需将以下行添加到Podfile中
pod "RicTaskHelper"
zLihuan, [email protected]
RicTaskHelper在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。