hmlasnk hmlasnk - 17 days ago 5
Swift Question

How to use observeValueForKeyPath for several NSProgress?

I have implemented file sending functionality for connected peers from loop using multiplier connectivity. That code as follows.

for connectedPeer in self.connectedPeers {
let sendingProgress = self.session.sendResourceAtURL(newPhotoUrl!, withName: "Image1", toPeer: connectedPeer, withCompletionHandler: { (error) in
if error != nil {
print("sending failed \(error!.localizedDescription) - \(connectedPeer.displayName)")
}
})
sendingProgress.addObserver(self, forKeyPath: "fractionCompleted", options: .New, context: nil)
}


It’s sending properly. I want to show each peer's sending progress on sender device. I can get progress from observeValueForKeyPath method. But how can I identify each sending progress individually from observeValueForKeyPath method.

Answer

You can use the parameter context for identifying the progress. Instead of nil you should give the url or the connected peer.