Search code examples
objective-ccocoamodal-dialogrunloop

runModalForWindow throttles http requests


I have url connection, which normally works fine

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:delegate];

But when I create a modal window, no request ever receives response:

[NSApp runModalForWindow:window];

If I comment this line out, thus creating a 'standard' window, everything works.

I tried implementing all methods from NSURLConnectionDelegate, not a single of them called.

I suspect this is something about 'run loops', but have little experience in this area. Does anybody have experience in this?

Thank you


Solution

  • If you're targeting 10.5+, you can tell the NSURLConnection to also run in NSModalPanelRunLoopMode (the mode your current thread's runloop would be in while presenting a modal view) via

    -(void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode
    

    where aRunLoop would probably be [NSRunLoop currentRunLoop] and the mode would be NSModalPanelRunLoopMode. More info in the NSURLConnection doc.

    If you're supporting earlier OSs, you may have to get creative (i.e. with multithreading). Good discussion of this issue pre-10.5 here.