gpt4 book ai didi

ios - NSMetadataQuery isUpdating 在 Reachability 更改为 none 后中断 Swift 4

转载 作者:行者123 更新时间:2023-11-30 11:17:27 24 4
gpt4 key购买 nike

我编写了一个 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()

}

}

}

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

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

返回错误

我没有考虑什么?

提前谢谢

最佳答案

我意识到尽管 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
}

还有其他想法吗?

关于ios - NSMetadataQuery isUpdating 在 Reachability 更改为 none 后中断 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51608957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com