1

Really odd problem I'm having: dataWithContentsOfURL has begun returning an error code 256 over cellular, but not over wifi.

The operation couldn’t be completed. (Cocoa error 256.)

I do indeed have a cellular data connection, and it is functioning, so it is not a problem with my cellular connection. Plus the code works fine on wifi, so the basic code is not the issue. The code in question is:

dispatch_queue_t queue = dispatch_queue_create("com.appName.FetchImage", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
                    ...
                    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
                    NSLog(@"URL: %@", url);
                    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
                    NSError *error = [[NSError alloc] init];
                    NSData *imgData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
                    if (error) NSLog(@"Error loading data: %@", error);
                    UIImage *image = [UIImage imageWithData:imgData];
                    ...
});

Any thoughts? I'm at a loss as to why this might be happening. It also occurs with the vanilla dataWithContentsOfURL (as opposed to with options).

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
w4th
  • 53
  • 1
  • 8
  • Try to see in the "Settings --> Cellular --> Use Cellular Data" if your app has cellular data enabled. This is a recent iOS feature and the "Celluar disabled for this app" alert is not always shown so it could generate confusion to the user. By they if you set cellular data off for this app and use the app under cellular those foundation methods will return error 256. – viggio24 Feb 22 '14 at 20:33
  • @viggio24 Good thought, but that's not it -- just double checked, and cellular data is indeed enabled for the app. – w4th Feb 22 '14 at 21:18
  • Your code leaks a NSError. You want `NSError * error = nil;` – tc. Feb 22 '14 at 23:13
  • Another check you can do is to use, under wi-fi, the network conditioning tool (accessible from Settings --> Developer) setting it to different latency and bandwidth conditions. Finally try using NSURLConnection directly, and on the main thread. Just to check that the issue is not related to dataWithContentsOfURL: only. – viggio24 Feb 23 '14 at 08:41

2 Answers2

4

NSCocoaErrorDomain Code = 256 means:

A file system or file I/O related error whose reason is unknown.

Simply, it tells us nothing.

However in general, NSData's dataWithContentsOfURL should only be use to access local file resources.

Important: Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

You can try improving your code and use a better way of downloading data. It might fix the issue you are experiencing. Instead of using dataWithContentsOfURL, you can use NSURLConnection's class methods like:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request 
                          queue:(NSOperationQueue *)queue 
              completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler;

Based on:

Using NSURLConnection

NSData Class Reference

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • This worked -- for whatever reason, NSURLConnection works, despite dataWithContentsOfURL on a background thread being the way loading images is recommended by a number of resources I've found. Thank you. – w4th Feb 24 '14 at 21:06
1

In my case, the issue was a bad network connection on my mobile 4G. It was not xcode-specific, it also existed in regular safari browsing. I switched to a different network and that fixed the problem. The "hint" was that the method did not return immediately - it timed out after about a minute.

Sagi Mann
  • 2,967
  • 6
  • 39
  • 72