5

I got a really annoying problem. Im trying to search after a NSNetService (that i know exist). I've copied the example code from http://www.macresearch.org/cocoa-scientists-part-xxviii-bonjour-and-how-do-you-do into my own application. I can see (in the example program) that my service is created. But when i hit search, and start a NSNetServiceBrowser search, nothing happens. The delegate function newer gets called

 -(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser didFindService:(NSNetService *)aService moreComing:(BOOL)more {

I've been checking stuff like that my objects are still retained, and i can't see that anything is released (and i don't have GC on). Anybody got any ideas what to do? The code is exactly the same as in the example, only i got some other stuff around it (running a openGL window).

Jonas Jongejan
  • 2,826
  • 1
  • 16
  • 17
  • Are you sure you have assigned the right delegate to the instance of NSNetServiceBrowser? – fbrunel Dec 20 '10 at 22:00
  • I have the same problem. But it only gets called when I publish the service and search it from the same device, but when testing it with two devices never finds the service. – Daniel Sanchez May 25 '12 at 00:13

1 Answers1

10

This question is eons old (iOS 4/5 era?) so I'm not sure if this is the answer for you, but make sure that you're using NSNetServiceBrowser from the main thread of your application only.

(I can't remember if it also must be initialized on the main thread, or if it must just be initialized and called -search on from the same thread, or if it suffices to call -search from the main thread, but I had the exact same problem and in my case it was related to using a background thread in some way. Sorry for the vague answer.)

TaylanKammer
  • 5,177
  • 1
  • 19
  • 16
  • Just tested this myself, you need to init NSNetService from the main thread itself too. Just assigning the delegate and doing a call such as NSNetService's "searchForServicesOfType..." within the main thread alone won't work. I threw the netservice stuff within a dispatch_async(dispatch_get_main_queue(), ^{ ... }); block. – Vivek Gani Aug 12 '14 at 01:07
  • 2
    Actually you don't need to use NSNetService on the main thread. You do need to make sure you call [[NSRunLoop currentRunLoop] run] on the thread you're initializing NSNetService on. The main runloop is running by default, but other threads may not be. – psilencer Jul 11 '18 at 08:30
  • And make sure you call -run after you start the search! Otherwise nothing will be added to the run loop, and start won't do anything. – Mojo66 Dec 02 '22 at 12:02