简体   繁体   English

如何在Swift中对NSProgress进行键值观察

[英]How to do Key-Value Observing for NSProgress in Swift

I'm currently using the owncloud iOS SDK to upload a file to my private cloud. 我目前正在使用owncloud iOS SDK将文件上传到我的私有云。 I'm trying to bring the Key-Value observing mechanism provided in this example to Swift. 我正在尝试将此示例中提供的Key-Value观察机制引入Swift。

The library forces me to pass a pointer to a NSProgress object in the upload method. 该库强制我在upload方法中传递指向NSProgress对象的指针。 I would then like to do a Key Value Observing and update my UI accordingly. 然后我想做一个Key Value Observing并相应地更新我的UI。

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)

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM