在子线程上创建一个NSNetServiceBrowser,并调用searchForServicesOfType开始搜索,是不会起作用。因为他需要在runloop中实现,苹果的文档有这么一段话:
A network service browser performs all searches asynchronously using the current run loop to execute the search in the background. Results from a search are returned through the associated delegate object, which your client application must provide. Searching proceeds in the background until the object receives a stop message.
To use an NSNetServiceBrowser object to search for services, allocate it, initialize it, and assign a delegate. (If you wish, you can also use the scheduleInRunLoop:forMode: and removeFromRunLoop:forMode: methods to execute searches on a run loop other than the current one.) Once your object is ready, you begin by gathering the list of accessible domains using either the searchForRegistrationDomains or searchForBrowsableDomainsmethods. From the list of returned domains, you can pick one and use the searchForServicesOfType:inDomain:method to search for services in that domain.
NSNetServiceBrowser是使用run loop实现不断循环搜索的,类似于NSTimer,主线程中默认开启了runloop,而子线程中默认没有创建runloop,所以需要自己创建并开启一个RunLoop,然后把NSNetServiceBrowser使用 scheduleInRunLoop:forMode: 添加到runloop里,这样Bonjour才能开始工作。