gpt4 book ai didi

ios - key volumeAvailableCapacityKey、volumeAvailableCapacityForImportantUsageKey 和 volumeAvailableCapacityForOpportunisticUsageKey 之间的区别

转载 作者:行者123 更新时间:2023-11-28 14:52:02 34 4
gpt4 key购买 nike

在将某些文件下载到我的应用程序之前,我正在尝试获取可用磁盘空间。在 iOS 11 中,我按如下方式执行此操作

let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)

if #available(iOS 11.0, *) {
// Running iOS 11 OR NEWER
let pathURL = URL(fileURLWithPath:paths.last!)
do {
let values = try pathURL.resourceValues(forKeys: [.volumeAvailableCapacityKey])
if let capacity = values.volumeAvailableCapacityForImportantUsage {
return capacity;
} else {
return nil;
}
} catch {
return nil
}
}

现在我的问题是-

volumeAvailableCapacityKeyvolumeAvailableCapacityForImportantUsageKeyvolumeAvailableCapacityForOpportunisticUsageKey 之间有什么区别?我应该使用什么来获取设备中的实际可用空间?

最佳答案

根据 Apple 文档:

volumeAvailableCapacityKey
Key for the volume’s available capacity in bytes (read-only)

volumeAvailableCapacityForImportantUsageKey
Key for the volume’s available capacity in bytes for storing important resources (read-only)

volumeAvailableCapacityForOpportunisticUsageKey Key for the volume’s available capacity in bytes for storing nonessential resources (read-only)

volumeTotalCapacityKey
Key for the volume’s total capacity in bytes (read-only)

我建议您阅读此 Apple doc很小心。它突出显示:

Overview

Before you try to store a large amount of data locally, first verify that you have sufficient storage capacity. To get the storage capacity of a volume, you construct a URL (using an instance of URL) that references an object on the volume to be queried, and then query that volume.

Decide Which Query Type to Use

The query type to use depends on what's being stored. If you’re storing data based on a user request or resources the app requires to function properly (for example, a video the user is about to watch or resources that are needed for the next level in a game), query against volumeAvailableCapacityForImportantUsageKey. However, if you’re downloading data in a more predictive manner (for example, downloading a newly available episode of a TV series that the user has been watching recently), query against volumeAvailableCapacityForOpportunisticUsageKey.

还有一个非常有趣的例子:

let fileURL = URL(fileURLWithPath:"/")
do {
let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
if let capacity = values.volumeAvailableCapacityForImportantUsage {
print("Available capacity for important usage: \(capacity)")
} else {
print("Capacity is unavailable")
}
} catch {
print("Error retrieving capacity: \(error.localizedDescription)")
}

关于ios - key volumeAvailableCapacityKey、volumeAvailableCapacityForImportantUsageKey 和 volumeAvailableCapacityForOpportunisticUsageKey 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49775866/

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