为什么"volumeAvailableCapacityForImportantUsage"为零?

编程入门 行业动态 更新时间:2024-10-28 00:17:12
本文介绍了为什么"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"为零?

本文发布于:2023-11-26 19:39:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为零   quot   volumeAvailableCapacityForImportantUsage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!