[NSURLConnection sendSynchronousRequest:returningResponse:error:]

I had a lot of trouble with this beast in Tiger and it came up in Leopard again. As I did not find any solutions elsewhere, here's how I fixed it for me.

Problem: NSURLConnection sendSynchronousRequest:returningResponse:error starts a download and blocks the calling thread until all bytes are received from the stream. In most cases this just works. But for some reason, if you call this on the main thread, it sometimes does not unlock the thread resulting in an application hang.
Lesson Learned: Fully synchronous url requests are impossible. Always load networks resources from a background thread.

Next problem: Code Data loads an XML data store using this method. So it's no wonder that sometimes, when the arraycontroller gets filled, your application just hangs. Or, under Tiger, crashes because the NSData the store loads the XML into gets deallocated before the store is even finished loading.

Solution: To fix this, I wrote a category for NSXMLDocument that loads file resources using standard file methods. Otherwise it does what I expect it to do in the default implementation.

- (id)initWithContentsOfURL:(NSURL *)url options:(unsigned int)mask error:(NSError **)error
id result = nil;
NSData *data = nil;
if ([url isFileURL])
data = [[NSData allocinitWithContentsOfFile:[url path]];
NSHTTPURLResponse *response = NULL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];
result = [self initWithData:data options:mask error:error];
return result;