Multipeer Connectivity Framework - Lost Peer stays in Session

后端 未结 5 749
鱼传尺愫
鱼传尺愫 2020-12-23 15:36

I wonder if this Multipeer Connectivity framework is ready for use in the real world, given all the bugs that have been encountered by the community. I think I\'m setting it

5条回答
  •  温柔的废话
    2020-12-23 16:06

    I've been having similar problems. It seems though that if I have run my app on one iOS device, and connected to another, then quit and relaunch (say when I rerun from Xcode), then I am in a situation where I get a Connected message and then a Not Connected message a little later. This was throwing me off. But looking more carefully, I can see that the Not Connected message is actually meant for a different peerId than the one that has connected.

    I think the problem here is that most samples I've seen just care about the displayName of the peerID, and neglect the fact that you can get multiple peerIDs for the same device/displayName.

    I am now checking the displayName first and then verifying that the peerID is the same, by doing a compare of the pointers.

    - (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
    
        MyPlayer *player = _players[peerID.displayName];
    
        if ((state == MCSessionStateNotConnected) &&
            (peerID != player.peerID)) {
            NSLog(@"remnant connection drop");
            return; // note that I don't care if player is nil, since I don't want to
                    // add a dictionary object for a Not Connecting peer.
        }
        if (player == nil) {
            player = [MyPlayer init];
            player.peerID = peerID;
            _players[peerID.displayName] = player;
        }
        player.state = state;
    
    ...
    

提交回复
热议问题