描述
数据请求框架封装,具有NSOperation系统行为,可方便的进行线程间通信。
它基于AFN作为基础数据请求类,同时功能层和逻辑层的分离设计也使你很方便的更换核心请求库。请求类作为NSOperation的子类可以让你与系统的其他NSOperation子类任意搭配使用。另外,他还扩展了批量请求以及请求链功能,满足你日常数据请求的基本需要。
Description
这是一个具有与NSOperation相同行为的请求框架,可以实现线程间的通信。
它使用AFN作为核心请求类,同时通过功能层和逻辑层的分离设计,方便地更换核心请求库。请求类作为NSOperation的子类,允许您与其他系统中的NSOperation子类进行任意组合。另外,它还扩展了批量请求和请求链功能,以满足您日常数据请求的基本需求。
功能
- 与系统NSOperation配合使用
- 全局参数
- 批量请求与请求链
- 请求加密
- 请求缓存策略
Func
- 结合 NSOperation 使用
- 全局参数
- 批量请求和请求链
- 请求加密
- 请求缓存策略
如何使用
首先,你应该将所需文件拖入项目中,或者你也可以使用Cocoapods进行集成。
pod 'DWFlashFlow', '~> 1.0.0'
你可以这样使用单个请求:
DWFlashFlowRequest * r = [DWFlashFlowRequest new];
r.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"finish");
};
[r start];
或者使用DWFlashFlowManager,它是一个提供全局配置的管理者。
DWFlashFlowRequest * r = [DWFlashFlowRequest new];
r.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"completion");
};
[DWFlashFlowManager sendRequest:r completion:^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"finish");
}];
以此方式与NSOperation的其他操作结合使用:
DWFlashFlowRequest * r = [DWFlashFlowRequest new];
r.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"%@",response);
};
NSBlockOperation * bP = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"The request complete.");
}];
[bP addDependency:r];
[[NSOperationQueue new] addOperations:@[bP,r] waitUntilFinished:NO];
批量请求的示例如下:
DWFlashFlowRequest * r1 = [DWFlashFlowRequest new];
r1.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r1.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"r1 finish");
};
DWFlashFlowRequest * r2 = [DWFlashFlowRequest new];
r2.fullURL = @"http://ozi0yn414.bkt.clouddn.com/MKJ-Time.mp3";
r2.requestProgress = ^(NSProgress *progress) {
NSLog(@"%@",progress);
};
r2.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"r2 finish");
};
r2.requestType = DWFlashFlowRequestTypeDownload;
DWFlashFlowBatchRequest * bR = [[DWFlashFlowBatchRequest alloc] initWithRequests:@[r1,r2]];
[DWFlashFlowManager sendRequest:bR completion:^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"%@",response);
}];
请求链:
DWFlashFlowRequest * r1 = [DWFlashFlowRequest new];
r1.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r1.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"r1 finish");
};
DWFlashFlowRequest * r2 = [DWFlashFlowRequest new];
r2.fullURL = @"http://ozi0yn414.bkt.clouddn.com/MKJ-Time.mp3";
r2.requestProgress = ^(NSProgress *progress) {
NSLog(@"%@",progress);
};
r2.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"r2 finish");
};
r2.requestType = DWFlashFlowRequestTypeDownload;
DWFlashFlowChainRequest * cR = [[DWFlashFlowChainRequest alloc] initWithRequests:@[r1,r2]];
[DWFlashFlowManager sendRequest:cR completion:^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"%@",response);
}];
其他一些使用方法,你可以查看.h文件中的注释。
Usage
首先,将其拖到你的项目中或使用Cocoapods集成。
pod 'DWFlashFlow', '~> 1.0.0'
发送请求,你可以这样做:
DWFlashFlowRequest * r = [DWFlashFlowRequest new];
r.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"finish");
};
[r start];
或者使用管理者。
DWFlashFlowRequest * r = [DWFlashFlowRequest new];
r.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"completion");
};
[DWFlashFlowManager sendRequest:r completion:^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"finish");
}];
以此方式与其他NSOperation结合:
DWFlashFlowRequest * r = [DWFlashFlowRequest new];
r.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"%@",response);
};
NSBlockOperation * bP = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"The request complete.");
}];
[bP addDependency:r];
[[NSOperationQueue new] addOperations:@[bP,r] waitUntilFinished:NO];
批量请求的例程如下:
DWFlashFlowRequest * r1 = [DWFlashFlowRequest new];
r1.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r1.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"r1 finish");
};
DWFlashFlowRequest * r2 = [DWFlashFlowRequest new];
r2.fullURL = @"http://ozi0yn414.bkt.clouddn.com/MKJ-Time.mp3";
r2.requestProgress = ^(NSProgress *progress) {
NSLog(@"%@",progress);
};
r2.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"r2 finish");
};
r2.requestType = DWFlashFlowRequestTypeDownload;
DWFlashFlowBatchRequest * bR = [[DWFlashFlowBatchRequest alloc] initWithRequests:@[r1,r2]];
[DWFlashFlowManager sendRequest:bR completion:^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"%@",response);
}];
链请求:
DWFlashFlowRequest * r1 = [DWFlashFlowRequest new];
r1.fullURL = @"https://www.easy-mock.com/mock/5ab8d2273838ca14983dc100/zdwApi/test";
r1.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"r1 finish");
};
DWFlashFlowRequest * r2 = [DWFlashFlowRequest new];
r2.fullURL = @"http://ozi0yn414.bkt.clouddn.com/MKJ-Time.mp3";
r2.requestProgress = ^(NSProgress *progress) {
NSLog(@"%@",progress);
};
r2.requestCompletion = ^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"r2 finish");
};
r2.requestType = DWFlashFlowRequestTypeDownload;
DWFlashFlowChainRequest * cR = [[DWFlashFlowChainRequest alloc] initWithRequests:@[r1,r2]];
[DWFlashFlowManager sendRequest:cR completion:^(BOOL success, id response, NSError *error, DWFlashFlowAbstractRequest *request) {
NSLog(@"%@",response);
}];
其他一些功能,你可以在.h文件中的注释中找到。
联系作者
你可以在我的Github上给我留言或发送电子邮件 [email protected] 来提出建议或指出bug,我将不胜感激。
如果你喜欢这个工具,请不要忘记给我一个star,么么哒~
Contact With Me
你可以在 我的Github 上向我提交问题或通过 [email protected] 发送邮件,告诉我一些建议或指出bug,我会很感激。
如果你喜欢它,请给我一个star。