问题
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:
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.
来源:https://stackoverflow.com/questions/26923570/when-we-are-working-with-mcsessionstate-didchangestate-responds-very-slowly-we