How do I use initWithUpgradeForSession safely?

It's a little bit unclear to be how to use initWithUpgradeForSession safely.

Imagine we have a class like this:

@implementation PPNUDPSessionPipeWrapper {
  NWUDPSession *_session;
  NWUDPSession *_upgradedSession;
}

- (void) startReadingPackets() {
[_session
      setReadHandler:^(NSArray<NSData *> *datagrams, 
                                 NSError *error) {   
     // process datagrams
    [_session writeMultipleDatagrams:datagrams
                  completionHandler:^(NSError *error) {
          // Handle error
     }];
}];

-(void)didHaveBetterPath {
  _upgradedSession = 
    [[NWUDPSession alloc] 
      initWithUpgradeForSession:_session];  
  [self waitForSessionToBeReady: _upgradedSession];
  // How can I swap `_session` with the 
  // `_upgradedSession` safely? Will iOS guarantee the 
  // readHandler on `_session` will not be called any 
  // more?
}

The first thing I would try is upgrading your session. Once that is available, cancel your existing session and assign your new session to a strongly retained value of your existing session and then setup your reads and writes again. You will probably lose some data here until your new session gets setup, but it's better than running into issues trying to balance both sessions at one time.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
How do I use initWithUpgradeForSession safely?
 
 
Q