PromiseKit+AFNetworking {#welcome}
PromiseKit+AFNetworking 是针对令人愉悦的 PromiseKit 的小类别扩展,使其能够与广泛使用的 AFNetworking v 2.0一起工作
目前,它是对 AFNetworking 核心的一个小类别扩展,有助于使用纯 AFHTTPRequestOperation 和 AFHTTPRequestOperationManager 进行开发。
关于 PromiseKit 2.0 的说明
PromiseKit+AFNetworking
现在支持 PromiseKit 2.0
。因此,iOS 7 的支持已被删除。如果您希望具有 PromiseKit + AFNetworking + iOS 7 支持,请使用小于 0.5.0 版本的 'AFNetworking-PromiseKit'
关于 PromiseKit 3.0 的说明
PromiseKit+AFNetworking
现在支持 PromiseKit 3.0
。对于 PromiseKit 3.0,解析了 PMKPromise 和 AnyPromise 之间的歧义,现在只使用 AnyPromise。如果希望有 PromiseKit + AFNetworking + PromiseKit 2.0 支持,请使用小于 0.6.0 版本的 'AFNetworking-PromiseKit'
安装
您有两种选择:或者使用cocoapods(参见下面的适当部分),或者只需将AFNetworking+PromiseKit.h
和AFNetworking+PromiseKit.m
复制到您的项目中并使用它们。
样本使用
普通HTTP请求
[AFHTTPRequestOperation request:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://oramind.com/"]]].then(^(id responseObject){
NSLog(@"operation completed! %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
}).catch(^(NSError *error){
NSLog(@"error: %@", error.localizedDescription);
NSLog(@"original operation: %@", error.userInfo[AFHTTPRequestOperationErrorKey]);
});
上面的代码将立即通过将其添加到当前队列(如果当前队列不存在,则为主队列)来执行HTTP请求。您也可以将其添加到不同的队列(参见头文件)
AFHTTPRequestOperationManager
self.manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:nil];
self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[self.manager GET:@"http://www.error-url.ss/" parameters:nil].then(^(id responseObject, AFHTTPRequestOperation *operation){
NSLog(@"first request completed for operation: %@", operation.request.description);
return [self.manager GET:@"http://www.apple.com" parameters:nil];
}).then(^{
NSLog(@"second request completed");
}).catch(^(NSError *error){
NSLog(@"error happened: %@", error.localizedDescription);
NSLog(@"original operation: %@", error.userInfo[AFHTTPRequestOperationErrorKey]);
});
由普通AFHTTPRequestOperationManager返回的对象也在此处作为可选参数返回,包括类型为'id'的响应对象以及AFHTTPRequestOperation。在发生错误的情况下,您可以使用PromiseKit的"catch"命令,并获取由AFNetworking抛出的错误和引发错误的原始操作'error.userInfo[AFHTTPRequestOperationErrorKey]'
使用因PromiseKit官方文档中描述的"-when"方法,使用PMKPromise进行并发操作是一件简单的事情。
[PMKPromise when:@[
[self.operationManager GET:@"ip" parameters:nil].then(^(){numberOfOperationsCompleted ++;}),
[self.operationManager GET:@"get" parameters:nil].then(^(){numberOfOperationsCompleted ++;})
]].then(^(){
//do something when all operations are finished
});
AFHTTPSessionManager
新特性:现在支持AFHTTPSessionManager,适用于AFNetworking 2.0和iOS 7
下面的示例函数展示了如何使用AFHTTPSessionManager进行登录
- (PMKPromise *)loginWithUserName:(NSString *)userName andPassword:(NSString *)password
{
return [self.sessionManager POST:@"api/login" parameters:@{@"user": userName, @"password" : password}].then(^(id responseObject, NSURLSessionDataTask *dataTask){
//responseObject holds the response returned by AFNetworking
//dataDask will hold an NSURLSessionDataTask associated with this request, like it happens in AFHTTPSessionManager
});
}
在此类操作中返回的对象为可选。在"then()"块中的最大参数数量为"2",一个已经被AFNetworking反序列化的responseObject和一个NSURLSessionDataTask。
当然,链式调用和并发操作的工作方式与AFHTTPRequestOperationManager相同。
when
获取引发了请求的 NSURLDataTask
。
现在,在使用 when
执行多个并发请求时,您可以获取引发了请求的 NSURLDataTask
。
[self.sessionManager GETMultiple:@[@"/", @"/", @"/", @"/"] parameters:@[@{},@{},@{},@{}]].then(^(NSArray * responsesArray){
for (NSDictionary *responseDictionary in responsesArray){
NSLog(@"task description: %@", responseDictionary[kPMKAFResponseTaskKey]);
NSLog(@"response object description: %@", responseDictionary[kPMKAFResponseObjectKey]);
}
});
[self.operationManager GETMultiple:@[@"/", @"/", @"/", @"/"] parameters:@[@{},@{},@{},@{}]].then(^(NSArray * responsesArray){
for (NSDictionary *responseDictionary in responsesArray){
NSLog(@"operation description: %@", responseDictionary[kPMKAFResponseOperationKey]);
NSLog(@"response object description: %@", responseDictionary[kPMKAFResponseObjectKey]);
}
});
示例
现在有单元测试可用,它展示了基本功能并提供了一些如何使用此类型的示例。未来还将添加更多单元测试。
Pods集成
您可以使用CocoaPods使用该项目。只需将此行放入您的podfile中
pod 'PromiseKit-AFNetworking', '~>0.6'
待办事项
- 添加更多单元测试
- 在某些情况下添加一些有用的功能。
特别感谢
特别感谢Jeff Miller(https://github.com/jeffmax)对项目的修改和将其repo转换为Pod
- Michael Lo(https://github.com/mmmilo)
- Roberto(https://github.com/robertomg)
- Jeff Miller(https://github.com/jeffmax)
当然,还要感谢Max Howell(https://github.com/mxcl)PromiseKit
免责声明
我创建了这个小插件是为了在我的项目中使用它。我有机会在实际场景中进行测试,但是AFHTTPRequestOperationManager尚未经过DELETE和PUT操作的测试。如果您发现了一个错误,请提交一个错误报告。
缺少什么?
当然有东西缺少。我刚刚开始实现这个功能。如果您有建议、补充或想添加酷炫功能,我非常愿意听取。请,请,请在“问题”部分留下您的反馈和建议!