Note that there are some explanatory texts on larger screens.

plurals
  1. POSynchronous Bonjour resolution with NSNetService (why is it taking so long to return control from the runloop?)
    primarykey
    data
    text
    <p>I am trying to do a Bonjour resolution of a service name synchronously, so I am trying to figure out how to properly wait for the netServiceDidResolveAddress() call of NSNetService.</p> <p>My code looks like this:</p> <pre><code>service = [[NSNetService alloc] initWithDomain:@"local." type:@"_myservice._tcp" name:[self name]]; [service retain]; [service setDelegate:self]; waitingOnResolution = true; [service resolveWithTimeout:5.0]; for (int i=0;i&lt;5;i++) { DebugLog(@"resolve: resolve iteraton %u\n", i); [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]]; // run loop for one second DebugLog(@"resolve: resolve iteraton %u (after run loop)\n", i); if (!waitingOnResolution) break; } // continue processing... </code></pre> <p>And here is my function that handles the resolution success:</p> <pre><code>- (void)netServiceDidResolveAddress:(NSNetService *)sender { // do processing... waitingOnResolution = false; CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]); // has no effect } </code></pre> <p>As long as my code continues to run a second or so after the resolve event comes in, I'll be happy. </p> <p>What is actually happening is the resolve happens pretty quickly, and I see the code in netServiceDidResolveAddress() complete, but then there is consistently an undesirable 5-6 second wait until control returns from runUntilDate and I see the "...(after run loop) debug statement.</p> <p>I looked around on several message boards and saw similar questions, but their solutions (like using CFRunLoopStop) don't seem to have any effect. </p> <p>Here is an example of logging I receive: </p> <pre><code>2011-07-11 09:38:07.267 MyProgram[1822:707] resolve: resolve iteraton 0 2011-07-11 09:38:07.299 MyProgram[1822:707] netService: netServiceDidResolveAddress function ended [5+ second delay] 2011-07-11 09:38:12.907 MyProgram[1822:707] resolve: resolve iteraton 0 (after run loop) </code></pre> <p>If someone can tell me of a different way to use NSNetService resolution to get a synchronous result, or refactor my runloop-related code to avoid this (seemingly OS-created) 5-6 second delay, I would appreciate it. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload