Multipeer Connectivity IOS:didFinishReceivingResourceWithName 错误处理(崩溃)

Posted

技术标签:

【中文标题】Multipeer Connectivity IOS:didFinishReceivingResourceWithName 错误处理(崩溃)【英文标题】:Multipeer Connectivity IOS: didFinishReceivingResourceWithName Error Handling (Crash) 【发布时间】:2017-03-07 19:08:44 【问题描述】:

我一直在努力尝试在相对较短的时间内让 Multipeer Connectivity 在我们的应用中运行。大多数事情进展得相当顺利,但我们现在遇到了一个非常令人费解的问题。

当我们遵循快乐的路径时,所有数据都可以正常传输,但是在尝试执行错误处理时……这是通过关闭 wifi 中间传输来完成的……我的代码……:

分享者:

func sendResource(data: Data?, name: String, fileName: String, peerId: MCPeerID)
        if data != nil
            let url = createTransferFile(jsonData: data!, name: fileName)

            if url != nil
                session.sendResource(at: url!, withName: name, toPeer: peerId, withCompletionHandler:  (error) -> Void in
                    if error != nil
                        NSLog("Error in sending resource send resource: \(error!.localizedDescription)")
                    
                )

            
        
    

接收者:

func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL, withError error: Error?) 
        NSLog("%@", "didFinishReceivingResourceWithName: \(resourceName)")
        if error != nil
            NSLog("error in receiving")
        
        if resourceName.contains("clinicDetails")
            if error == nil
                if let data = self.readClinicJsonFromFile(path: localURL)
                    NSLog("passing to broadcast delegate")
                    sendDelegate?.addClinicDetails(self, clinicDetailsJSON: data)

                else
                    NSLog("there was an error in finding the retrieved file in clinic retrieve finished")
                    _ = Util.showAlert(retrievePatientDelegate as! UIViewController, code: 3021, actions: nil, isCustom: true) as! AlertController
                
            else
                _ = Util.showAlert(retrievePatientDelegate as! UIViewController, code: 3021, actions: nil, isCustom: true) as! AlertController
            
        else if resourceName.contains("patients")
            //NSLog("clinicId in retrievePatient: \(json["clinicId"])")
            if error == nil
                if let data = self.readPatientJsonFromFile(path: localURL)
                    NSLog("passing to retrieve patients delegate")
                    retrievePatientDelegate?.addPatients(self, patientJSON: data , clinicId: resourceName.components(separatedBy: "/")[1])
                else
                    NSLog("there was an error in finding the retrieved file in patient retrieve finished")
                     _ = Util.showAlert(retrievePatientDelegate as! UIViewController, code: 3021, actions: nil, isCustom: true) as! AlertController

                
            else
                _ = Util.showAlert(retrievePatientDelegate as! UIViewController, code: 3021, actions: nil, isCustom: true) as! AlertController
            
        else if resourceName == "clinicList"
            if error == nil
                if let data = self.readClinicListJsonFromFile(path: localURL)
                    NSLog("passing to retrieve retrieveDelegate")
                    retrieveDelegate?.addClinics(self, clinicsJSON:  data["jsonData"] as! [[String:Any]], passcode: data["passcode"] as! String)
                else
                    NSLog("there was an error in finding the retrieved file in patient retrieve finished")
                    _ = Util.showAlert(retrievePatientDelegate as! UIViewController, code: 3021, actions: nil, isCustom: true) as! AlertController
                
            else
                _ = Util.showAlert(retrievePatientDelegate as! UIViewController, code: 3021, actions: nil, isCustom: true) as! AlertController
            
        
    

我们收到的错误:

2017-03-06 16:52:54.416352 DC[2445:1444277] [GCKSession] Failed to send a DTLS packet with 78 bytes; sendmsg error: Can't assign requested address (49).
2017-03-06 16:52:54.416560 DC[2445:1444277] [GCKSession] SSLWrite failed, packet was not sent for participant [05280B9E] channelID [4] DTLS context [0x103043ea0] pCList [0x10e94f960]; osStatus = -9803: errno = Can't assign requested address (49).

这些行会根据剩余的进度量打印出来。

Then we also get the following stack in xcode(我还不能将图片直接添加到我的帖子中:

Stack Frame from the thread causing the error

【问题讨论】:

您能分享该跟踪中的堆栈帧吗?该线程崩溃时正在运行什么代码? 好的会尽快处理 堆栈帧是什么意思?你想让我给你发一个调试器会话的屏幕截图吗?还是有更好的发送方式? @戴夫韦斯顿 在 Xcode 左侧的面板中,其中一个将显示以该崩溃结束的堆栈帧。它将列出崩溃发生时执行的所有方法,最新的在顶部。然后我们会想看看你写的触发崩溃的代码。您共享的图像包含实际发生崩溃的机器代码,但这并不能告诉我们我们是如何到达那里的。 @DaveWeston 嘿,我附加了导致崩溃的线程的堆栈帧.. 够好还是需要所有线程? 【参考方案1】:

由于 didFinishReceivingResourceWithName 函数中的 localURL 不是可选的,因此此错误似乎与 apples 框架中的错误有关。错误或取消进度的值为 nil。在查看崩溃线程中的每个调用然后找到相关的SO Post 后,我得出了这个结论

【讨论】:

以上是关于Multipeer Connectivity IOS:didFinishReceivingResourceWithName 错误处理(崩溃)的主要内容,如果未能解决你的问题,请参考以下文章

Multipeer Connectivity 自动静默连接

如何检测 Multipeer Connectivity 邀请的拒绝?

无法通过 Multipeer Connectivity 连接蓝牙

如何使用swift 4查看Multipeer Connectivity框架中的MCBrowserViewController

如何使用 Multipeer Connectivity 安全地停止发送图像

通过 Multipeer Connectivity 发送 MPMediaItem