【问题标题】:I am trying to run a function when ubiquitousItemDownloadingStatusKey is 'Current'当 ubiquitousItemDownloadingStatusKey 为“当前”时,我正在尝试运行一个函数
【发布时间】:2020-01-06 20:01:48
【问题描述】:

我正在尝试在我的应用的 iCloud 通用容器中下载一个目录,我想在下载完成后调用一个函数。我可以得到这样的状态:

func synciCloud(){
    do { try FileManager.default.startDownloadingUbiquitousItem(at: documentsPath)
        do {
            let status = try documentsPath.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey])
            print(status)
        }
        catch let error { print("Failed to get status: \(error.localizedDescription)") }
    }
    catch let error { print("Failed to download iCloud Documnets Folder: \(error.localizedDescription)") }
}

控制台打印:

URLResourceValues(_values: [__C.NSURLResourceKey(_rawValue: NSURLUbiquitousItemDownloadingStatusKey): NSURLUbiquitousItemDownloadingStatusCurrent],_keys: 设置([__C.NSURLResourceKey(_rawValue: NSURLUbiquitousItemDownloadingStatusKey)]))

我很困惑的是 NSMetadata 的语法以及如何执行我的函数。我的第一个想法是做类似下面代码的事情,但我不知道该怎么做:

if status.ubiquitousItemDownloadingStatus == ???? { function() }

任何建议将不胜感激。

谢谢。

【问题讨论】:

    标签: swift icloud nsfilemanager


    【解决方案1】:

    您还可以通过NSMetadataQuery 通知监控下载状态。

    let query = NSMetadataQuery()
    query.predicate = NSPredicate(format: "%K == %@", NSMetadataItemPathKey, documentsPath) // create predicate to search for you files
    
    NotificationCenter.default.addObserver(forName: .NSMetadataQueryDidUpdate, object: query, queue: nil) { notification in
      for i in 0..<query.resultsCount {
        if let item = query.result(at: i) as? NSMetadataItem {
          let downloadingStatus = item.value(forAttribute: NSMetadataUbiquitousItemDownloadingStatusKey) as! String
          print(downloadingStatus)
    
          if downloadingStatus == URLUbiquitousItemDownloadingStatus.current.rawValue {
            // file is donwloaded, call your function
          }
        }
      }
    }
    
    query.start() // starts the search query, updates will come through notifications
    
    // Once we are listening for download updates we can start the downloading process
    try? FileManager.default.startDownloadingUbiquitousItem(at: documentsPath)
    

    【讨论】:

    • 感谢 Palli,这很有帮助。下载完成时是否可以使用相同的方法进行侦听?
    • 是的,它在示例中:if downloadingStatus == ... 你知道该文件已完成下载。您还可以通过let percentDownloaded = item.value(forAttribute: NSMetadataUbiquitousItemPercentUploadedKey) as! Double 监控进度
    【解决方案2】:

    status.ubiquitousItemDownloadingStatus 的值为URLUbiquitousItemDownloadingStatus。这具有三个值。 .current 表示文件是最新的。

    if status.ubiquitousItemDownloadingStatus == .current {
        // it's up-to-date
    }
    

    【讨论】:

    • 那是完美的 rmaddy,谢谢。在您看来,这是下载文件后执行功能的最佳方式吗?这意味着我必须执行类似 if status.. != .current 之类的操作,然后再次运行它,直到它运行为止,然后运行该函数。但这似乎有点低效。我希望文件很小但仍然。
    • 有了这个你需要做一些轮询。我不知道在下载完成时收到通知的方法,但可能有一些方法。
    猜你喜欢
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多