zoukankan      html  css  js  c++  java
  • AFHTTPClient的异步回调模式

    以前第一个版本,ios的http都用的同步模式,在很多地方会导致线程阻塞,自己开发了一个简易的AFHTTPClient的异步回调模式。

    回调的protocol:

    @protocol MyAFNetworkingResponse <NSObject>

    @required

    -(void) MyHttpResponse:(NSString*)ret Type:(NSString*)strType returnData:(NSObject*)retData;

    @end

     

    AFHTTPClient的异步通信的实现类:

    @interface MyAFNetworkingClient : AFHTTPClient

    @property NSURL* mServerUrl;//http服务地址

    @property id<MyAFNetworkingResponse> delegate;//这个最重要,回调的代理

     

    //类初始化函数

    - (id)init;

    - (id)initWithBaseURL:(NSURL *)url;

     

    /*然后是一堆实现的函数*/

    .....

    @end

     

    然后viewcontroller中:

    @interface MainViewController ()< MyAFNetworkingResponse>

    @end

    @implementation MainViewController

    - (void)viewDidLoad {

        [super viewDidLoad];    

        //初始化

        self.mAFNetworkingHttpClient = [[MyAFNetworkingClient alloc] init];

        self.mAFNetworkingHttpClient.delegate = self;

    }

     

    -(void) MyHttpResponse:(NSString*)ret Type:(NSString*)strType returnData:(NSObject*)retInfo{

        __weak MainViewController* wself = self;

        if ([strType isEqualToString:@"xxxx"]) {

             //某个具体http类型的,实现处理

        }else if([strType isEqualToString:@"xxxx"]){

              //某个具体http类型的,实现处理

        }

    }

    @end

  • 相关阅读:
    as3 的相关资源
    linux 进程用户栈和内核栈
    Chapter 11 进程与信号 @ linux
    linux/unix下setuid/seteuid/setreuid/setresuid
    poj 3259 spfa 虫洞问题判到点1时候有环
    My Vimrc Archive
    C/C++函数调用的几种方式
    Git常用命令解说 [robby certification]
    Linux Chapter 11 进程与信号
    XNA游戏开发之(四)——改变Draw频率
  • 原文地址:https://www.cnblogs.com/runner42/p/4630994.html
Copyright © 2011-2022 走看看