When we are working with MCSessionState, didChangeState responds very slowly we could not find why

258 views Asked by At

We are using following code to inform user about peer to peer connection state. But there is a problem "labelState.text = state.displayName" changes label text almost 10 seconds after " println ("State Changed to \(state.displayName)" is showed the state. Is there anyone faces the same problem.

  func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState)
{

    println("State Changed to \(peerID.displayName)")

    labelState.text = peerID.displayName
 }
1

There are 1 answers

0
ChrisH On BEST ANSWER

MCSessionDelegate callbacks do not come on the main thread. If you are making UI changes in that function you need to do so on the main thread.

dispatch_async(dispatch_get_main_queue()) {
    labelState.text = state.displayName
}

You should also be using displayName on the MCPeerID object not the MCSessionState which is just an enum.