NSBundleResourceRequest没有进度更新

时间:2017-04-15 14:14:27

标签: ios request resources

当我尝试观察NSBundleResourceRequest的进度时,observeValue(forKeyPath: object: change: context:)观察选项未调用.new。因此,NSProgressIndicator未更新。这是设置和代码:

设定:      Xcode 8.3.1      部署目标iOS 10.3      设备:iPad 4      资源标记(368 KB)位于“仅按需下载”中,由UICollectionView中显示的缩略图组成。缩略图图像位于MyCollection.xcassets。所有IBOutlets都已连接。

图像在集合视图中正确显示,但进度条保持为零。

代码:

 final class MyCollectionVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

          var myCollectionVCResourceRequest: NSBundleResourceRequest!
          var myCollectionVCResourceRequestLoaded = false
          static var myCollectionProgressObservingContext = UUID().uuidString

          private let designThumbnailTags: Set<String> = ["Resource1", "Resource2", "Resource3"]

          override func viewWillAppear(_ animated: Bool) {
                  super.viewWillAppear(animated)
                  loadOnDemandResources()
         }

          func loadOnDemandResources() {
                  myCollectionVCResourceRequest = NSBundleResourceRequest(tags: designThumbnailTags)
                  myCollectionVCResourceRequest.progress.addObserver(self, forKeyPath: "fractionCompleted", options: [.new, .initial], context:                &MyCollectionVC.myCollectionProgressObservingContext)

                  myCollectionVCResourceRequest.beginAccessingResources(completionHandler: { (error) in
                 print("Complete: \(self.myCollectionVCResourceRequest.progress.fractionCompleted)")  // Prints Complete: 0.0
                 self.myCollectionVCResourceRequest.progress.removeObserver(self, forKeyPath: "fractionCompleted", context: &MyCollectionVC.myCollectionProgressObservingContext)

              OperationQueue.main.addOperation({
                     guard error == nil else { self.handleOnDemandResourceError(error! as NSError); return }

                     self.myCollectionVCResourceRequestLoaded = true
                     self.updateViewsForOnDemandResourceAvailability()
                     self.fetchDesignThumbnailsWithOnDemandResourceTags(self.myCollectionVCResourceRequest) // Correctly creates Core Data Instances
                 })
        })
    }

         override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
             if  context == &MyCollectionVC.myCollectionProgressObservingContext {
                 OperationQueue.main.addOperation({
                     let progressObject = object as! Progress
                     self.progressView.progress = Float(progressObject.fractionCompleted)
                     print(Float(progressObject.fractionCompleted))  // Prints 0.0 as a result of including the .initial option
                     self.progressDetailLabel.text = progressObject.localizedDescription
                 })
             }
             else {
                 super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
             }
         }
    }

0 个答案:

没有答案