0

I am using the latest Swift documentation to write NetService, which am able to publish and search. But I am unable to remove service from the publisher. Not able to understand the role of RunLoop here. However, if I kill the app then service is getting removed.

This is the code am using to publish the service.

class ServicePublisher : NSObject {
    var nsNetService : NetService!
    init?(domain : String, type : String, name : String, port : Int32){
        nsNetService = NetService(domain: domain, type: type, name: name, port: port)
        if nsNetService == nil{
            return nil
        }
    }
    func publish() {
        nsNetService.publish(options:NetService.Options.listenForConnections)
    }
    func setDelegate(delegate : NetServiceDelegate) {
        nsNetService.delegate = delegate
    }
    func stop(){
        nsNetService.stop()
    }
}
Tarun Chawla
  • 508
  • 4
  • 17
  • `stop` is not immediate, actually all NetService methods are asynchronous, you delegate `netServiceDidStop` will inform you when service got stopped. As NetService by nature is asynchronous, RunLoop is needed for processing events from it, RunLoop for NetService can be run in background thread or be default one. – Asperi Mar 18 '20 at 11:04
  • netServiceDidStop is never called after I call stop. Am not able to understand the role of RunLoop here. – Tarun Chawla Mar 18 '20 at 11:06
  • @Asperi I did not attached the button to the function which was supposed to call stop on NetService and therefore the issue. But I did not need to configure RunLoop for that and service is getting removed and net service browser is also able to detect that service is removed. – Tarun Chawla Mar 18 '20 at 11:20

1 Answers1

0

nsNetService.stop() should stop the service and get it removed from the DNS SD. NetServiceBrowser is also able to detect the removal of service. My mistake was that I did not attached the UIButton with the function which was supposed to call stop() api and hence no service removal.

Tarun Chawla
  • 508
  • 4
  • 17