Nitu
Nitu

Reputation: 41

Can we stop synchronous request made? How?

It would be greatly appreciated if anyone could help me how to stop a synchronous request made to the server, especially while moving from background to foreground.

Thanks in advance.

Upvotes: 1

Views: 898

Answers (2)

PeyloW
PeyloW

Reputation: 36752

Are we talking about +[NSURLConnection sendSynchronousRequest:returningResponse:error:]? In that case you can not stop this request.

If you need to potentially stop a network operation in mid flight, then you need to use an asynchronous API. I personally prefer to roll my own using NSURLConnection and what is the least amount of work for the current task, usually a NSOperation or just some GCD. I strongly suggest that you learn how to use NSURLConnection even if you choose to use some other approach.

One such other approach is AFNetworking, that wraps NSURLConnection is a NSOperation that you can call cancel on.

Upvotes: 0

Wolfert
Wolfert

Reputation: 974

If you use a normal NSURLConnection you could just send it the cancel command. (docs)

init like:

 NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

cancel like so:

[connection cancel];

Upvotes: 1

Related Questions