无法在多点连接框架中显示数据传输进度

Unable to display data transfer progress in multipeer connectivity framework

我正在尝试打印使用多点连接时的数据传输进度。 progress 信息在接收方 didStartReceivingResourceWithName 方法和发送方 sendResource 方法中可用。 以下是我如何实现接收方:

func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
        DispatchQueue.main.async {
            print (progress)
        }
    }

这是我实现发送端的方式:

func sendFileAction()->Progress{
        var filePath = Bundle.main.url(forResource: "10MO", withExtension: "file")
        if mcSession.connectedPeers.count > 0 {

            do {
                let data =  try Data(contentsOf: filePath!)
                fileTransferProgressInSender = mcSession.sendResource(at: filePath!, withName: "filename", toPeer: mcSession.connectedPeers[0]) { (error) -> Void in
                    DispatchQueue.main.async {
                        if error != nil {
                            print("Sending error: \(String(describing: error))")
                        }else{
                            print("sendAFile with no error "+"filename")
                        }
                    }
                }
            }
            catch let error as NSError {
                let ac = UIAlertController(title: "Send file error", message: error.localizedDescription, preferredStyle: .alert)
                ac.addAction(UIAlertAction(title: "OK", style: .default))
                present(ac, animated: true)
            }
        }
        return(fileTransferProgressInSender)
    }

receiver 函数只在开始时显示 progress 一次。

<NSProgress: 0x1c0133740> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 10485760  

而且我不知道在哪里可以调用 sendFileAction 的 return 来显示发送方的进度。

有什么帮助吗? 谢谢。

编辑: 我尝试使用以下代码:

func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
        startTime = Date()

        DispatchQueue.main.async {
            self.endTransfer = false
            self.sendProgressBar.progress = 0.0
            self.updateProgress(progress: progress)
        }
    }

func updateProgress(progress:Progress){
        DispatchQueue.main.async {
            while !self.endTransfer {
                print (progress.fractionCompleted)
                self.sendProgressBar.progress = Float(progress.fractionCompleted)
            }
        }
    }

虽然 print 在控制台中给出了实际进度,但进度条从 0 跳到 1(在 print 之前达到 1)。

我做错了什么?

再次感谢。

对于你的接收者,你应该捕获 Progress 变量来保存,然后通过重复计时器查询它。

Apple 概述了 basics here

An NSProgress object that can be used to cancel the transfer or queried to determine how far the transfer has progressed.

对于您的发件人,您正在从您的函数中获取进度指示器作为 return。保存它并使用计时器查询它以查找状态。

感谢@CodeBender 的提示。确实,我需要一个计时器。 所以这就是我的做法:

func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
        startTime = Date()
        self.receptionProgress = progress
        DispatchQueue.main.async {
            self.receptionTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.updateReceptionProgress), userInfo: nil, repeats: true)
            self.receptionTimer.fire()
        }
    }

以及对应的函数:

@objc func updateReceptionProgress(){
        self.receptionProgressBar.progress = Float(self.receptionProgress.fractionCompleted)
        if self.receptionProgress.completedUnitCount >= self.receptionProgress.totalUnitCount{
            self.receptionTimer.invalidate()
        }
    }