4

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?

productioncoder
  • 4,225
  • 2
  • 39
  • 65

1 Answers1

6

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)

        }

    }
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
  • 1
    Just tried it but the method is never called. The overall upload works, the success callback is invoked but I cannot track the progress. – productioncoder May 03 '16 at 07:41