AFPromise
AFPromise 也被称为 AFNetworking + PromiseKit
提供一种更优雅的方式来处理 AFNetworking 回调
示例
firstly(^ {
isLoading = true;
return HttpBin.GET.promiseJSON;
})
.then(^(NSDictionary *JSON) {
// handle this
})
.catch(^(NSError *error)
//show erorr
)
.ensure(^{
isLoading = false;
})
如何使用
AFPromise 为您提供了 3 个抽象请求类来使用
-
HttpRequest
您可以用来做大多数网络请求,如 GET / POST / PUT
-
UploadRequest
您可以用来处理数据上传工作,提供三种上传方法和进度监控,以便您可以在上传数据时跟踪上传进度。
例如
firstly(^ { return UploadRequest.data(^{ return someData; }) .uploadProgress(^(NSProgress *progress) { // observer progress }) .promiseJSON; }) .then(^(id JSON) { // });
-
DownloadRequest
您可以使用它下载和恢复大文件。当然,下载进度监控也是必不可少的。
例如
firstly(^{ return DownloadRequest.downloadToDestination(^(NSURL *target, NSURLResponse *res) { return destination; }) .resumeData(^ { return reuseData; }) .downloadProgress(^(NSProgress *progress) { // }) .promiseData; }) .then(^(NSURL *fileURL) { // });
还有两个额外的结果处理方法来帮助我们更方便地处理结果数据
-
promiseData
原始请求结果
-
promiseJSO
将原始数据解析为 JSON
对于更高级的使用,请参阅源代码实现
注意
尚未在production项目中使用,目前只有小版本可供使用。但是,我已经在测试用例上做了很多工作,请根据需要使用AFPromise。
安装
pod "AFPromise", "0.1.2"