开源
CCRequest:基于AFNetworking 3.x的iOS和MacOS dance 的HTTP请求框架
来源:元经纪     阅读:590
网站管理员
发布于 2023-02-14 08:42
查看主页

概述

基于AFNetworking 3.x的iOS和MacOS dance 的HTTP请求框架。

特征

安装

用法

[hidecontent type="logged" desc="隐藏内容:登录后可查看"]

Promise.then

// Start first request task.

SamplePHPRequest.new.promise.then(^id(id data){

    // Get result(`Model` or `RawData`) finish by first task.
    // Then start sencond request task within promise.
    
    return SamplePHPRequest.new.promise;
    
},^id(CCResponseError *reason){
    
    // If some bad things happend to first task you can catch it in this scope.
    // But the error can't stop the promise chain.
    // Exception(or some thing) will send to next promise(if exist) in return value.
    
    return reason;
    
}).then(^id(id data){
    
    // Got result(`Model` or `RawData` or `Exception`) finish by previous promise.
    // Then start third request task within promise.
    // The result finish by third task will not be deal by Fornt End (Just send to server whithout care succeed or not).

    return [[[SamplePHPRequest new] bindRequestArgument:nil] promise];
    
},NULL);

Promise.next

// start primise chain

SamplePHPRequest.new.promise.next(^id(id data) {
    

    // Get result(`Model` or `RawData`) finish by first promised task.
    // These scope will never get errors.    
    CCLogInfo(@"%@",data);
    
    // Send data to next node in promise chain encapsulated in return value
    return data;
    
}).next(^id(id data){
    
    // Get result finish by previous promised node.    
    // Then start third request task within promise.

    return SamplePHPRequest.new.promise;
    
}).next(^id(id data) {
    
    // Get result(`Model` or `RawData`) finish by previous promised node.    
    CCLogInfo(@"Promise chain succeed");
    
    // Must return something, but will not sent to the `catch` node
    return data;
    
}).catch(^(CCResponseError *reason) {
    
    // If some bad things happend to `the whole promise chain`, you can catch it in this scope.
    // Peomise chain will be interrupted when catching any exception, 
    // which meanse the rest of promise nodes will not be excuted forever.

    CCLogError(@"Promise chain failed: %@",reason);
});

####Promise.all

[CCPromise all:@[SampleRequest.new.promise, SamplePHPRequest.new.promise]].then(^id(id data) {
    
    // If all of the promsie succeed, you can catch their result in this scope.
    // The data is an unordered array.
    // Maybe Dictionary is more appropriate, the key is ordered by index in promises.

    CCLogInfo(@"Get data: %@",data);
    
    // Must return something.
    // I want to optimize it, please send me any suggestions.
    return CCPromise.fulfilled;
    
}, ^id(CCResponseError *reason) {
    
    // If some bad things happend to `any promise in anof the promise`, you can catch it in this scope.
    CCLogError(@"Catched Error: %@",reason);
    return CCPromise.rejected;
    
});

####Normal Way

SamplePHPRequest *request = [SamplePHPRequest new];
[[request requestWithSuccess:^(id result, CCRequest *request) {
    
} failure:^(CCResponseError *error, CCRequest *request) {
    CCLogInfo(@"Never invoke the callback if you cancel the request");
}] appendAccessory:self];

####Surported Cache Policy

typedef NS_ENUM(NSUInteger, CCRequestCachePolicy) {
    
    // Request server immediately
    CCRequestReloadRemoteDataIgnoringCacheData,
    
    // Searching cache data first, return if hited data; otherwise request server data
    CCRequestReturnCacheDataElseReloadRemoteData,
    
    // Searching cache data first, invoke callbacks if hited data
    // No matter succeed or not we request server data
    CCRequestReturnCacheDataThenReloadRemoteData,
    
    // Request server data first, searching cache data if failed
    CCRequestReloadRemoteDataElseReturnCacheData,
};

// Cache Hitting Policy:

typedef NS_ENUM(NSUInteger, CCReturnCachePolicy) {
    
    // hit by fire time
    CCReturnCacheDataByFireTime,
    
    // hit by revalidating fired time if exist
    CCReloadRevalidatingCacheData
};

// Cache Write Policy:

typedef NS_ENUM(NSUInteger, CCDataCachePolicy) {
    
    // Cache Models 
    // Implement NSCoding protocol if you use the default cache service
    CCCachePolicyModel,
    
    // Cache raw data
    // JSON object for CCResponseSerializerTypeJSON
    // RawData for CCResponseSerializerTypeRawData
    CCCachePolicyRawData,
};

####Other advantages
  • 支持实现您的请求自定义验证器
  • 支持自定义授权或 HTTP 标头
  • 在面对令人眼花缭乱的服务器框架时节省时间

[/hidecontent]

 
免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 开源
华硕推出三款vu系列显示器,集成纳米离子技术
JSCAD:适用于3D打印应用的命令行工具
行业首家12年无忧服务,海信冰箱引领家电服务新标准
元宇宙中的虚拟市场有怎么样的发展前景?
家庭空气净化器选购技巧

首页

分类

定制方案

消息

我的