为什么"volumeAvailableCapacityForImportantUsage"为零? [英] Why is `volumeAvailableCapacityForImportantUsage` zero?

查看:48
本文介绍了为什么"volumeAvailableCapacityForImportantUsage"为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循 Apple记录的示例,以了解如何查询设备上的可用磁盘空间.

I'm following the Apple's documented example to figure out how to query the available disk space on my device.

我在我的 applicationDidFinishLaunchingWithOptions 中使用以下代码:

I'm using the code below in my applicationDidFinishLaunchingWithOptions:

let fileURL = URL(fileURLWithPath:"/")
do {
    let values = try fileURL.resourceValues(forKeys: [
        .volumeAvailableCapacityKey,
        .volumeAvailableCapacityForImportantUsageKey,
        .volumeAvailableCapacityForOpportunisticUsageKey,
        .volumeTotalCapacityKey
    ])
    print("Available Capacity: \(Float(values.volumeAvailableCapacity!)/1000000000)GB")
    print("ImportantUsage Capacity: \(Float(values.volumeAvailableCapacityForImportantUsage!)/1000000000)GB")
    print("Opportunistic Capacity: \(Float(values.volumeAvailableCapacityForOpportunisticUsage!)/1000000000)GB")
    print("Total Capacity: \(Float(values.volumeTotalCapacity!)/1000000000)GB")
} catch {
    print("Error retrieving capacity: \(error.localizedDescription)")
}

这将记录以下内容:

Available Capacity: 3.665879GB
ImportantUsage Capacity: 0.0GB
Opportunistic Capacity: 0.0GB
Total Capacity: 63.989494GB

为什么 volumeAvailableCapacityForImportantUsage volumeAvailableCapacityForOpportunisticUsage 为零,在什么情况下会发生这种情况?

Why are volumeAvailableCapacityForImportantUsage and volumeAvailableCapacityForOpportunisticUsage zero and under what circumstances does this happen?

背景:

  • 我正在通过xCode 10.2.1在自己的64GB iPhone SE上运行此实验(因此总容量看起来正确)
  • 我的iPhone正在运行iOS11
  • iTunes声称我的设备具有10.27GB的免费"空间
  • 我正在尝试弄清楚这一点,以便我知道我的用户是否将有足够的空间来下载大量(超过40MB)的应用内购买商品

注意:这与

Note: This is not the same as this question. I know how to query available space. I want to understand the results of that query.

推荐答案

问题是,您正在尝试获取/作为文件系统根目录的系统卷的容量.该API的行为很奇怪,但是您可以获得所需的信息.如果您使用应用程序的文档目录,则 FileManager 方法和 .volumeAvailableCapacityKey 仍会产生奇怪的值,但是您现在可以获得 .volumeAvailableCapacityForImportantUsageKey .volumeAvailableCapacityForOpportunisticUsageKey .

The issue is that you're attempting to get the capacity for the system volume at / which is the root of the filesystem. The behaviour of this API is strange but you can get the info you need. If you use your app's documents directory then the FileManager method and .volumeAvailableCapacityKey will still yield strange values but you now get useful values for .volumeAvailableCapacityForImportantUsageKey and .volumeAvailableCapacityForOpportunisticUsageKey.

示例:

let f = ByteCountFormatter()

let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
print("path = \(path)")

let attrs = try! FileManager.default.attributesOfFileSystem(forPath: path)
let fmFree = (attrs[.systemFreeSize] as! NSNumber).int64Value
print("FileManager.attributesOfFileSystem free: \(f.string(fromByteCount: fmFree))")

let docsURL = URL(fileURLWithPath: path)
let values = try! docsURL.resourceValues(forKeys: [.volumeAvailableCapacityKey, .volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityForOpportunisticUsageKey])
print("Volume available capacity: \(f.string(fromByteCount: Int64(values.volumeAvailableCapacity!)))")
print("Volume important available capacity: \(f.string(fromByteCount: values.volumeAvailableCapacityForImportantUsage!))")
print("Volume opportunistic available capacity: \(f.string(fromByteCount: Int64(values.volumeAvailableCapacityForOpportunisticUsage!)))")

在我的系统上打印:

FileManager.attributesOfFileSystem free: 8.51 GB
Volume available capacity: 8.51 GB
Volume important available capacity: 177.16 GB
Volume opportunistic available capacity: 175.88 GB

这篇关于为什么"volumeAvailableCapacityForImportantUsage"为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆