Why I cannot call searchForBrowsableDomains and searchForServicesOfType in the same function?

769 Views Asked by At

I think the use of the Runloop is probably problematic. I am not sure if it is related to the problem I have below:

Here is my code:

import Foundation



class Discover: NSObject, NSNetServiceBrowserDelegate {
    var browser:NSNetServiceBrowser;
    var done:Bool = false;

    init() {
        browser = NSNetServiceBrowser();
        super.init();
        browser.delegate = self;
    };

    func netServiceBrowser(aNetServiceBrowser: NSNetServiceBrowser!, didFindDomain domainString: String!, moreComing: Bool) {
        println("Find domain \(domainString)");
    }

    func netServiceBrowser(aNetServiceBrowser: NSNetServiceBrowser!, didFindService aNetService: NSNetService!, moreComing: Bool) {
       // println(aNetService);
        println("Find servcie \(aNetService)");
    }

    func discover () {
        browser.searchForBrowsableDomains()
        browser.searchForServicesOfType("_http._tcp", inDomain: "local"); 
    }
}


let d = Discover();
d.discover();

let runloop = NSRunLoop.currentRunLoop();
runloop.run();

println("Done");

I find that, in the discover function, if I call either searchForBrowsableDomains or searchForServicesOfType, I can obtain result in the delegate functions.

However if I call one after another, the delegate functions are never called.

Why it is the case?

0

There are 0 best solutions below