Skip navigation.
 
mlRe: runloops and NSURLConnection
FROM : Jean-Daniel Dupas
DATE : Sun Jun 01 18:18:27 2008

Le 1 juin 08 à 14:27, Torsten Curdt a écrit :

>> If you handle the modal session yourself, it's easy to let the 
>> connection do some processing while the modal window is open:
>> Replace -[NSApp runModalForWindow:[self window]] by this and it 
>> should do the trick.
>>
>>    /* Create a modal session, and in each loop,
>>    we process the default runloop event sources (url download, 
>> network connections, etc.) */
>>    NSModalSession session = [NSApp beginModalSessionForWindow:[self 
>> window]]; // 1
>>    for (;;) {
>>      if ((result = [NSApp runModalSession:session]) != 
>> NSRunContinuesResponse) // 2
>>        break; // 3
>>      /* Note: Do not use a 0 timeout, else this loop will never 
>> block and will consume a lots of CPU.
>>      In fact, the Cocoa UI event port is scheduled in the default 
>> runloop, so each Cocoa event will wakeup the runloop. */


       [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
beforeDate:[NSDate distantFuture]];

>>      //[[NSRunLoop currentRunLoop] runUntilDate:[NSDate 
>> distantFuture]]; // 4
>>    }
>>    [NSApp endModalSession:session];

>
> So that basically means I create my own run loop and call out into 
> the non-modal one from time to time, right?
> Just wondering about the "from time to time" part.
>
> cheers
> --
> Torsten
>


Argh, This code does not works (there is no launch and test button in 
Mail :-)).
You have to call [[NSRunLoop currentRunLoop] 
runMode:NSDefaultRunLoopMode  beforeDate:[NSDate distantFuture]];
else it will never returns.


You never create a runloop. Each thread has a "current loop" which 
support recursive call to "run".

This snippet create a modal session.  (//1)
Then it check the modal session state and process UI events if some 
events are ready ([NSApp runModalSession:session]) (//2)
Note that runModalSession: does not block and always returns immediatly.

If the user stop the modal session, it stop the loop (//3)

After that it blocks until something append (network event, ui event, 
etc). (//4)
Each time a network event occurs, the run loop pass it to the handler 
(NSURLConnection in your case).
Each time a UI event occurs, the run loop push it on the UI event 
queue and the next call the runModalSession: process it.
Each time an event occurs, runMode:beforeDate: returns.

Related mailsAuthorDate
mlrunloops and NSURLConnection Torsten Curdt May 31, 15:28
mlRe: runloops and NSURLConnection Michael Vannorsdel May 31, 16:09
mlRe: runloops and NSURLConnection Torsten Curdt May 31, 17:07
mlRe: runloops and NSURLConnection Jens Alfke May 31, 18:55
mlRe: runloops and NSURLConnection Michael Vannorsdel May 31, 19:03
mlRe: runloops and NSURLConnection Torsten Curdt May 31, 19:14
mlRe: runloops and NSURLConnection Jens Alfke May 31, 19:54
mlRe: runloops and NSURLConnection Jean-Daniel Dupas May 31, 19:58
mlRe: runloops and NSURLConnection Torsten Curdt Jun 1, 14:27
mlRe: runloops and NSURLConnection Torsten Curdt Jun 1, 14:28
mlRe: runloops and NSURLConnection Jean-Daniel Dupas Jun 1, 18:18