iOS中Bonjour:NSNetServiceBrowser在子线程上使用

来源:互联网 发布:飞思卡尔竞赛编程软件 编辑:程序博客网 时间:2024/04/29 23:58

    如果在子线程上创建一个NSNetServiceBrowser,并调用searchForServicesOfType开始搜索,发现不起作用。查看苹果的文档发现这么一段话:

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 thescheduleInRunLoop: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 thesearchForRegistrationDomains or searchForBrowsableDomains methods. From the list of returned domains, you can pick one and use thesearchForServicesOfType:inDomain: method to search for services in that domain.

    也就是说NSNetServiceBrowser是使用run loop实现不断循环搜索的,类似于NSTimer,主线程中默认开启了runloop,而子线程中默认没有创建runloop,所以需要自己创建并开启一个RunLoop,然后把NSNetServiceBrowser使用scheduleInRunLoop:forMode: 添加到runloop里,这样Bonjour才能开始工作。
0 0
原创粉丝点击