title: GKSession won't send data to peers on second connection
body: <p>I've got a peer to peer iPhone game working pretty well. I've created a table to display available peers and I can connect (so far) up to four devices and play a full round of a game from start to finish:</p>
  
  <p><a href="https://i.stack.imgur.com/4mg6S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4mg6S.png" alt="peer picker"></a>
  </p>
  
  <p>But here's my problem...</p>
  
  <p>I create a <code>GKSession</code> on one device with <code>GKSessionModeClient</code> and the other with <code>GKSessionModeServer</code>. The client sees the server, selects to connect it, then the server approves. The server then can tap "start game" when all the peers are connected.</p>
  
  <p>If--before the server starts the game-- the client taps the "back" button to go to the previous screen, the custom peer picker display is popped from the view and the session is "destroyed"...</p>
  
  <pre><code>[_gkSession disconnectFromAllPeers];
  [_gkSession setAvailable:NO];
  [_gkSession setDelegate:nil];
  [_gkSession setDataReceiveHandler:nil withContext:nil];
  </code></pre>
  
  <p>now, if the client goes back to the peer picker and joins the server again, I get a <code>GKSessionStateConnected</code> state again, but when I <code>sendDataToAllPeers:</code> nothing reaches the client. It works flawlessly if one device starts a game and three devices join and then the server taps "start game". But if someone quits out before the game starts, then recreating a session and joining fails. </p>
  
  <p>Any ideas? My only hack I can think of at this point is preventing a user from tapping "back" once they connect to a server, but that kind of sucks...they'd have to quit the app completely if they changed their mind. I'm at my wits end with this one as the app is running as well as I like it except for this connection issue. Kind of an edge case but I can see a user being annoyed if they can't start a game without having to restart the application to get the sessions cleared.</p>
  
  <p>Will make this a bounty when I can. Please help!</p>
  
  <p><strong>Edit @byteclub</strong></p>
  
  <blockquote>
    <ul>
    <li>Does the server clean up all of the relevant data structures correctly if
    client leaves before game begins?</li>
    </ul>
  </blockquote>
  
  <p>If the client leaves the game lobby after the server accepts their connection, the client's session is destroyed:</p>
  
  <pre><code>- (void)destroySession
  {
      trace(@"destroySession");
  
      self.gameDelegate = nil;
      self.lobbyDelegate = nil;
  
      [_gkSession disconnectFromAllPeers];
      [_gkSession setAvailable:NO];
      [_gkSession setDelegate:nil];
      [_gkSession setDataReceiveHandler:nil withContext:nil];
  
      [_peerList removeAllObjects];
  }
  </code></pre>
  
  <p>I do not call any disconnectFromPeers methods on the server device though.</p>
  
  <blockquote>
  <pre><code>* Is the client-disconnect-cleanup procedure different for cases when the
  </code></pre>
    
    <p>game has been started already?</p>
  </blockquote>
  
  <p>Slightly, if a client disconnects then everyone receives a "peer quit" notification and the game ends. I don't worry about reconnecting in this case. It's mainly in the game lobby where the client can "Cancel" to go back to the home screen where the problem is. The client's session is destroyed in this case. If they then go back to the lobby and a <em>new</em> GKSession is alloc'ed, they can connect to the server still, but sendDataToAllPeers no longer works once they are connected.</p>
  
  <blockquote>
  <pre><code>* Do you still have a problem if, before reconnecting, you restart the
  </code></pre>
    
    <p>client, but leave the server running
    as is?</p>
  </blockquote>
  
  <p>The client can reconnect in any case. I can quit, go back, restart the app and reconnect no problem. Just sendDataToPeers fails unless the server <em>and</em> client are both restarted.</p>
  
  <blockquote>
  <pre><code>* Do you completely destroy GKSession object on the client side
  </code></pre>
    
    <p>when user taps "back"?</p>
  </blockquote>
  
  <p>Yes. (see code above)</p>
  
  <p>I realize this is probably insane to debug from afar but if you have any suggestions for what I could look into, I'd appreciate it.</p>