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

戏子无情 提交于 2019-12-11 05:17:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!