NSMetadataQuery isUpdating 在可达性更改为无 Swift 4 后中断

Posted

技术标签:

【中文标题】NSMetadataQuery isUpdating 在可达性更改为无 Swift 4 后中断【英文标题】:NSMetadataQuery isUpdating breaks after Reachability changes to none Swift 4 【发布时间】:2018-07-31 08:35:47 【问题描述】:

我编写了一个UIViewController 来处理(仅)将文件上传到 iCloud。到目前为止它运行良好,但我试图通过使用Reachability 的网络更改使其更好。 我处理更改的方式是:

lazy var searchQuery:NSMetadataQuery = 
    let searchQueryTemp = NSMetadataQuery()
    searchQueryTemp.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]
    let searchPredicate = NSPredicate.init(format: "%K BEGINSWITH %@ && NOT %K.pathExtension = ''", argumentArray: [NSMetadataItemPathKey,trackFileManager.appICloudExportedMusic!.path,NSMetadataItemFSNameKey])
    searchQueryTemp.predicate = searchPredicate
    return searchQueryTemp
()

override func viewWillAppear(_ animated: Bool) 
    super.viewWillAppear(animated)
    print("\(logClassName): viewWillAppear")

    appDelegate.appReachabilityDelegate = self

    NotificationCenter.default.addObserver(self, selector: #selector(updateDataWithNotification), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: searchQuery)

    NotificationCenter.default.addObserver(self, selector: #selector(updateDataWithNotification), name: NSNotification.Name.NSMetadataQueryDidUpdate, object: searchQuery)




override func viewDidAppear(_ animated: Bool) 
    super.viewDidAppear(animated)

    updateSearch()



override func viewWillDisappear(_ animated: Bool) 
    super.viewWillDisappear(animated)
    print("\(logClassName): viewWillDisappear")

    NotificationCenter.default.removeObserver(self)




@objc func updateDataWithNotification(notification: NSNotification)

    print("\(logClassName): updateDataWithNotification \(searchQuery.results.count)")

    if let queryObject = notification.object as? NSMetadataQuery

        if searchQuery == queryObject

            print("\(logClassName): Query count = \(queryObject.results.count)")
            var iCloudAlbumArrayTemp = [FileIcloudAlbumHeader]()

            /* Get the query results [URL] only files */
            for result in queryObject.results

                /* Get Metadata for file */
                if let metadataItem = result as? NSMetadataItem

                    if let urlItem:URL = metadataItem.value(forKey: NSMetadataUbiquitousItemURLInLocalContainerKey) as? URL

                        if var urlItemPath = urlItem.path.components(separatedBy: "Exported Music").last
                            urlItemPath = String(urlItemPath.dropFirst())
                            let urlItemArray = urlItemPath.components(separatedBy: "/")

                            if urlItemArray.count == 3

                                var albumICloudTrack = AlbumICloudTrack(url: urlItem)

                                let isUpdated:Bool = metadataItem.value(forKey: NSMetadataUbiquitousItemIsUploadedKey) as! Bool
                                let isUpdating: Bool = metadataItem.value(forKey: NSMetadataUbiquitousItemIsUploadingKey) as! Bool
                                let isDownloading:Bool = metadataItem.value(forKey: NSMetadataUbiquitousItemIsDownloadingKey) as! Bool

                                if isUpdated
                                    albumICloudTrack.status = .available
                                
                                else
                                    if isUpdating
                                        let perInt = Int(metadataItem.value(forKey: NSMetadataUbiquitousItemPercentUploadedKey) as! Double)
                                        print("\(logClassName): isUpdating: PerInt = \(perInt)")
                                        let perDouble = metadataItem.value(forKey: NSMetadataUbiquitousItemPercentUploadedKey) as! Double
                                        print("\(logClassName): isUpdating: PerInt = \(perDouble)")

                                        albumICloudTrack.percentatge = Int(metadataItem.value(forKey: NSMetadataUbiquitousItemPercentUploadedKey) as! Double)

                                        albumICloudTrack.status = .updating
                                    
                                    else if isDownloading
                                        albumICloudTrack.status = .downloading
                                    
                                    else

                                        albumICloudTrack.status = .notAvailable


                                    
                                


                                /* Find Album */
                                var tempUrl = urlItem.deletingLastPathComponent()
                                let albumName = tempUrl.lastPathComponent
                                tempUrl = tempUrl.deletingLastPathComponent()
                                let artistName = tempUrl.lastPathComponent

                                //print("\(logClassName): Artist Name = \(artistName) && Album Name = \(albumName)")
                                let albumHeaderInex = findAlbumHeader(withArtistName: artistName, andAlbum: albumName, in: iCloudAlbumArrayTemp)
                                if albumHeaderInex != -1
                                    //print("\(logClassName): Appending Already exists")
                                    iCloudAlbumArrayTemp[albumHeaderInex].urlTrackArray.append(albumICloudTrack)
                                
                                else
                                    //print("\(logClassName): Creating New Header Album")
                                    var albumHeader = FileIcloudAlbumHeader(artistName: artistName, albumName: albumName, url: urlItem.deletingLastPathComponent())
                                    albumHeader.urlTrackArray.append(albumICloudTrack)
                                    iCloudAlbumArrayTemp.append(albumHeader)
                                


                            
                            else
                                print("\(logClassName): Discarting Item = \(urlItemPath)")
                            

                        

                    

                

            


            /* Copy content for updating Expanded status */
            for iCloudAlbumIndex in iCloudAlbumArray.indices

                for iCloudAlbumTempIndex in iCloudAlbumArrayTemp.indices

                    if iCloudAlbumArray[iCloudAlbumIndex].artistName == iCloudAlbumArrayTemp[iCloudAlbumTempIndex].artistName && iCloudAlbumArray[iCloudAlbumIndex].albumName == iCloudAlbumArrayTemp[iCloudAlbumTempIndex].albumName
                        iCloudAlbumArrayTemp[iCloudAlbumTempIndex].isSelected = iCloudAlbumArray[iCloudAlbumIndex].isSelected
                    

                

            

            iCloudAlbumArray.removeAll()
            for iCloudAlbumTempIndex in iCloudAlbumArrayTemp.indices

                iCloudAlbumArray.append(iCloudAlbumArrayTemp[iCloudAlbumTempIndex])
                iCloudAlbumArray[iCloudAlbumTempIndex].urlTrackArray.sort 
                    return $0.trackName < $1.trackName
                

            


            /* Reload table */
            iCloudExportsTableView.reloadData()

        

    


这里的主要问题是我在“文件”中看到文件正在更新,但是

let isUpdating: Bool = metadataItem.value(forKey: NSMetadataUbiquitousItemIsUploadingKey) as! Bool

返回假

我没有考虑什么?

提前谢谢你

【问题讨论】:

【参考方案1】:

我已经意识到,尽管 NSMetadataUbiquitousItemIsUploadingKey 返回 false,但 NSMetadataUbiquitousItemPercentUploadedKey 仍然返回一个数字,所以:

let perDouble = metadataItem.value(forKey: NSMetadataUbiquitousItemPercentUploadedKey) as? Double ?? 100.00

if isUpdated
    albumICloudTrack.status = .available

else
    if isUpdating || perDouble < 100.00

    print("\(logClassName): isUpdating: perDouble = \(perDouble)")

    albumICloudTrack.percentatge = Int(metadataItem.value(forKey: NSMetadataUbiquitousItemPercentUploadedKey) as! Double)

                                        albumICloudTrack.status = .updating
    

还有其他想法吗?

【讨论】:

以上是关于NSMetadataQuery isUpdating 在可达性更改为无 Swift 4 后中断的主要内容,如果未能解决你的问题,请参考以下文章

NSMetadataQuery数据库

UIDocument openWithCompletionHandler:强制 NSMetadataQuery 更新

如何限制从 NSMetadataQuery 返回的结果数量?

为啥检查 [NSMetadataQuery 类] 在 iOS 4.2.1 上崩溃?

使用 NSMetadataQuery 在目录中搜索文件

NSMetadataQuery 返回显然不存在的文件