多路连接IOS:didFinishReceivingResourceWithName错误处理(崩溃)

时间:2017-03-07 19:08:44

标签: ios swift multipeer-connectivity

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

我们在跟随快乐路径时会将所有数据传输得很好但是在尝试实现错误处理时...这是通过关闭wifi mid transfer来完成的..我的代码......:

共享器:

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

1 个答案:

答案 0 :(得分:1)

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

后,我得出了这个结论