Hi There,
NWUDPSession has a setReadHandler:maxDatagrams: method to register a callback for reading packets. I am wondering how can I pause this handler not to be called for a short period(pause it for a while)?
I can think of few options, but all of them seem to be bad.
2. Block the handler to be executed. For example:
Thanks.
NWUDPSession has a setReadHandler:maxDatagrams: method to register a callback for reading packets. I am wondering how can I pause this handler not to be called for a short period(pause it for a while)?
I can think of few options, but all of them seem to be bad.
Cancel the session when paused, and create a new one when resumed.
Code Block -(void)stopReadingPackets { [self.session cancel]; } -(void)resume { self.session = ... [self.session setReadHandler:^(NSArray<NSData*>* datagrams, NSError* error) { // Handle packets here. } maxDatagrams:1]; }
2. Block the handler to be executed. For example:
Code Block [self.session setReadHandler:^(NSArray<NSData*>* datagrams, NSError* error) { while (self.paused) {} // Handle datagrams here. } maxDatagrams:1];
Thanks.