0

I used NSURLConnectionobject and called its method cancel sometimes.

Now I should replace NSURLConnection -> NSURLSession. NSURLSession operates with tasks which have cancel method too.

The problem is -[NSURLConnection cancel] just stop the handling of requests but if I use -[NSURLSessionTask cancel] it produces "cancelling error". So how to properly distinguish if cancel is called manually or if a real error is occurred?

Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49

2 Answers2

0

I've found 3 solutions:

  • subclass task/session class

  • create a custom property via method swizzling in task/session class

  • the simplest but not very beautiful way - task has string property taskDescription and documentation says that developers are free to use it as they want.

Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49
0

Compare the error code to NSURLErrorCancelled. This error code is generated only when your code cancels the request.

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • again - there is no info in documentation that it is sent by my code – Vyachaslav Gerchicov May 02 '16 at 07:29
  • I wrote many of those docs years ago, so that's probably my fault. The URL loading system does not, AFAIK, ever cancel requests. Requests either succeed or fail. There are only three ways I know of to get that error code: by calling the completion handler block in your didReceiveResponse method with `NSURLSessionResponseCancel`, by calling the `cancel` method on the task, or possibly by calling the authentication completion handler block with `NSURLSessionAuthChallengeCancelAuthenticationChallenge`, and I'm not certain about that last one. In all three cases, your code did the canceling. – dgatwood May 02 '16 at 07:37