NSURLConnection still calls delegate AFTER cancel method has been called

前端 未结 2 684
再見小時候
再見小時候 2020-12-18 14:44

Having a problem with NSURLConnection, if I create a NSURLConnection and call [connection connectionWithRequest] let it load a little then call [connection cancel] most of t

2条回答
  •  情歌与酒
    2020-12-18 15:04

    I have not yet run into this problem, but this could also work without tying up your delegate object:

    Since all delegate methods receive the calling Connection object as a parameter and you also know your actual active Connection object (or nil), simply ignore the delegation actions by comparing the two objects. This way a cancelled "ghost" Connection object can still call the delegate but not interfere with its internals.

    - (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data
    {
        if(connection != _URLConnection){return;}
        ...
        [_incomingData appendData:data];
        ...
    }
    

    where _URLConnection is a property in your delegate set to an active Connection, or nil.

提交回复
热议问题