I'm currently using the owncloud iOS SDK to upload a file to my private cloud. I'm trying to bring the Key-Value observing mechanism provided in this example to Swift.
The library forces me to pass a pointer to a NSProgress
object in the upload method. I would then like to do a Key Value Observing and update my UI accordingly.
class OwnCloudManager : NSObject {
//Key Value observation requires the dynamic keyword
//NSProgress must also extend from NSObject
dynamic var progress: NSProgress = NSProgress()
let apiURL : NSString = "https://cloud.example.com/remote.php/webdav/"
let ocCommunication = OCCommunication()
override init() {
//configure ocCommunication here, set user name, password
//and adjust ocCommunication.securityPolicy
//...
}
deinit {
//remove observer when object is destroyed
progress.removeObserver(self, forKeyPath: "fractionCompleted")
}
func uploadFile(folderURL : NSURL, filename: String) -> NSURLSessionUploadTask?{
let fileURL = apiURL.stringByAppendingPathComponent(filename)
let progressPointer = AutoreleasingUnsafeMutablePointer<NSProgress?>.init(&progress)
let uploadTask = ocCommunication.uploadFileSession(folderURL.path, toDestiny: fileURL, onCommunication: ocCommunication,
withProgress: progressPointer,
successRequest: ({(response, redirectedServer) in
//...
}),
failureRequest: ({ (response, redirectedServer, error) in
//request failed while execution
}),
failureBeforeRequest: ({ error in
//failure before request is executed
})
)
progress.addObserver(self, forKeyPath: "fractionCompleted", options: .New, context: nil)
uploadTask.resume()
return uploadTask
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if let observedProgress = object as? NSProgress where keyPath == "fractionCompleted" {
//call my delegate here that updates the UI
}
else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
}
I've set a breakpoint in the observation method but unfortunately it is never called. Can someone tell me how to fix this?
See this code for Progressing..!!
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "fractionCompleted"{
let progress : NSProgress = object as! NSProgress
print(progress)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With