zoukankan      html  css  js  c++  java
  • ASIHttprequest 报错

    (void)requestReceivedResponseHeaders:(NSMutableDictionary *)newResponseHeaders { 
      if ([self error] || [self mainRequest]) { return; }  
    --> if (delegate && [delegate respondsToSelector:didReceiveResponseHeadersSelector]) {

    先是封装了一个ASIHttprequest 异步调用的类,但是在每次打开调用这个类的viewController的时候会报错,不是全部报错,报错的位置位于上面这一条。

    一直百思不得其解,类似的问题有

    http://stackoverflow.com/questions/5701132/asihttprequest-crashes-my-app

    http://www.open-open.com/lib/view/open1363230763484.html

    http://www.cocoachina.com/bbs/read.php?tid=253887

    看过之后,确定封装的类没有问题,问题在于ARC过早的释放了ASIHttprequest的对象。

    原来的引用的代码

    BIDJsonConn *conn=[[BIDJsonConn alloc]intiwithurl:mainurl]
        conn.delegateforConn=self;
        NSString *inUrl=[[NSString alloc]initWithFormat: @"GetUserID/%@",_thisuser.username];
        [conn GetdataByJsonAsy:inUrl];

    get的异步访问方法

    -(void)GetdataByJsonAsy:(NSString *)inUrl{
        NSMutableString *str_serverUrl=[[NSMutableString alloc]initWithString:self.mainUrl];
        [str_serverUrl appendString:inUrl];
        NSURL *url=[NSURL URLWithString:str_serverUrl];
        ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self];
        [request startAsynchronous];
    }

    高手们应该一眼能看出这个问题

    BIDJsonConn 的实例conn在这里实例化后运行完这一段代码很可能就被释放了。这个时候异步完成回调的时候就找不到对象了,因为就会报错

    将conn声明为变量

    @interface BIDWelcomeViewController : UIViewController<JsonConnDeleage>
    {
        BIDJsonConn *conn;
    }

    在viewload中实例化,就不会再报错了。

    这个问题其实不是只局限于ASIHttprequest 的异步调用,适合很多异步调用的例子都需要注意这个问题,需要将异步执行的对象声明为该类的变量

    AFNetworking比ASIHttprequest 新很多也支持ARC,下次还是看看AFNetworking吧

    著作权声明:本文由http://www.cnblogs.com/keithmoring/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

  • 相关阅读:
    java学习网址大全
    Js 提示框
    api帮助文档及常见IT学习网站
    传参给 jsp
    jsp>action
    二级横菜单显示+sitemesh母板应用
    request,session
    map>json
    清空image画布并改变大小填充背景色
    取汉字首字母方法
  • 原文地址:https://www.cnblogs.com/keithmoring/p/4129728.html
Copyright © 2011-2022 走看看