Search code examples
iosobjective-cnsmutableurlrequest

Can I stop/cancel an already running Asynchronous NSMutableURLRequest by setting it to nil?


As the question says, will the following help? I have not set any delegate to the request. Am I better off setting delegates?

request = nil;

Solution

  • No. NSMutableURLRequest can not 'run'. NSMutableURLRequest is just an object that stores url, body and some other options. To run it you should use NSURLConnection, example:

    NSURLConnection* your_connection = [NSURLConnection connectionWithRequest:your_request delegate:delegate];
    

    or NSURLSession which returns NSURLSessionTask, example:

    NSURLSessionDownloadTask *sessionTask = [NSURLSession downloadTaskWithRequest:your_request];
    

    To cancel connection/task you should call

    [your_connection cancel];
    

    or

    [sessionTask cancel];
    

    depending on what do you use.