解决方案 »

  1.   

    1.你收到这些回调的时候,在主线程,你可以通过这种方式得到主线程:[NSThread mainThread]
    2.调用cancel会回调didFailWithError,前提是你不是在delegate回调里调用的cancel。如didReceiveData这个回调方法,你如果在这个方法里调用cancel的话,则不会调用didFailWithError
      

  2.   

    1.回调方法是在主线程上执行。可想而知,有时我们需要在回调方法中修改页面UI. 修改UI只能是在主线程中处理
    2. 发送cancel消息不会调用任何的代理的方法。看一下下面的伪代码 
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
        NSInteger status = [(NSHTTPURLResponse *)response statusCode]; 
        
        if (status != 200) 
            [self cancel:connection]; 
    } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
           /////// to do your logic
    } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
        [self cancel:connection]; 

    - (void)cancel:(NSURLConnection *)connection { 
        [connection cancel]; 
        /////////to do your logic.
    }
    看苹果文档的解释
    After this method is called, the connection makes no further delegate method calls. If you want to reattempt the connection, you should create a new connection object.