Question

Say you're using NSURLSession dataTaskWithURL: ...

self.currentConnection =
  [sess dataTaskWithURL:goUrl
  completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
    [self.results addObjectsFromArray: [self yourDataConversionCode:data] ];

    dispatch_async(dispatch_get_main_queue(), ^{  if (after) after(); );
    }];

of course, if might need to cancel it. (Say, the user has changed a search term, or similar problem.) That's easy with...

-(void)cancelAnyCurrentBooksearch
  {
  [self.currentConnection cancel];
  self.currentConnection = nil;
  }

However, stupidly it still calls the completion block which is very annoying.

Now, here's the "documentation" for cancel :

/* -cancel returns immediately, but marks a task as being canceled.
 * The task will signal -URLSession:task:didCompleteWithError: with an
 * error value of { NSURLErrorDomain, NSURLErrorCancelled }.
 */
- (void)cancel;

Here's the problem...

What the hell does this mean:

an error value of { NSURLErrorDomain, NSURLErrorCancelled }

Can you have TWO values in an NSError??

If so, how do I check that the NSError was exactly this:

" { NSURLErrorDomain, NSURLErrorCancelled } "

whatever that means?

Note that if you do this:

self.currentConnection =
  [sess dataTaskWithURL:goUrl
  completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
    if( [error code] == NSURLErrorCancelled )
      {
      NSLog(@"  seemed to be CANCELLED programmatically?");
      return;
      }

    [self.results addObjectsFromArray: [self yourDataConversionCode:data] ];

    dispatch_async(dispatch_get_main_queue(), ^{  if (after) after(); );
    }];

It does "seem to work" - but it's hard to know of course.

So, how to check for this??

 * The task will signal -URLSession:task:didCompleteWithError: with an
 * error value of { NSURLErrorDomain, NSURLErrorCancelled }.

What does "two errors in curly braces" mean in documentation?

Does it mean either of those could happen?

I have no idea how one could determine the answer here for certain, it's undocumented.

Was it helpful?

Solution

An error value of { NSURLErrorDomain, NSURLErrorCancelled }

Can you have TWO values in an NSError??

Yes. It's an object. It has properties. If you check the documentation, you'll find it has both a domain and an code property.

If you wanted to check both, you'd do it as you might expect:

if ([error.domain isEqualToString:NSURLErrorDomain] && 
    error.code == NSURLErrorCancelled)

See the Error Handling Programming Guide for full details of error handling, including domains and codes.

Having said that, I think it's remotely improbable that anything other than NSURLErrorDomain is going to be associated with an NSURLSession data task completion handler with the code of NSURLErrorCancelled, so you should be fine just checking that as you are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top