axiom-storage-management-ref by charleswiltgen/axiom
npx skills add https://github.com/charleswiltgen/axiom --skill axiom-storage-management-ref目的:关于存储压力、清理策略、磁盘空间和 URL 资源值的全面参考 可用性:iOS 5.0+(基础功能),iOS 11.0+(现代容量 API) 上下文:回答“iOS 是否提供任何方式来将文件标记为‘最后手段才清理’?”
当您需要时,请使用此技能:
“iOS 是否提供任何方式来将文件标记为‘最后手段才清理’?”
答案:不直接提供,但 iOS 提供了两种方法:
基于位置的清理(隐式优先级):
tmp/ → 积极清理(随时)Library/Caches/ → 在存储压力下清理Documents/、Application Support/ → 永不清理广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
容量检查(显式策略):
volumeAvailableCapacityForImportantUsage — 用于必须保存的数据volumeAvailableCapacityForOpportunisticUsage — 用于可有可无的数据| 资源键 | 类型 | 目的 | 可用性 |
|---|---|---|---|
volumeAvailableCapacityKey | Int64 | 总可用空间 | iOS 5.0+ |
volumeAvailableCapacityForImportantUsageKey | Int64 | 用于重要文件的空间 | iOS 11.0+ |
volumeAvailableCapacityForOpportunisticUsageKey | Int64 | 用于可选文件的空间 | iOS 11.0+ |
volumeTotalCapacityKey | Int64 | 卷总容量 | iOS 5.0+ |
isExcludedFromBackupKey | Bool | 从 iCloud/iTunes 备份中排除 | iOS 5.1+ |
isPurgeableKey | Bool | 系统可在压力下删除 | iOS 9.0+ |
fileAllocatedSizeKey | Int64 | 实际使用的磁盘空间 | iOS 5.0+ |
totalFileAllocatedSizeKey | Int64 | 总分配空间(包括元数据) | iOS 5.0+ |
// ✅ 正确:保存前检查适当的容量
func checkSpaceBeforeSaving(fileSize: Int64, isEssential: Bool) -> Bool {
let homeURL = FileManager.default.homeDirectoryForCurrentUser
do {
let values = try homeURL.resourceValues(forKeys: [
.volumeAvailableCapacityForImportantUsageKey,
.volumeAvailableCapacityForOpportunisticUsageKey
])
if isEssential {
// 用于必须保存的数据(用户创建的内容、关键应用数据)
let importantCapacity = values.volumeAvailableCapacityForImportantUsage ?? 0
return fileSize < importantCapacity
} else {
// 用于可有可无的数据(缓存、缩略图)
let opportunisticCapacity = values.volumeAvailableCapacityForOpportunisticUsage ?? 0
return fileSize < opportunisticCapacity
}
} catch {
print("检查容量时出错:\(error)")
return false
}
}
// 用法
if checkSpaceBeforeSaving(fileSize: imageData.count, isEssential: true) {
try imageData.write(to: documentsURL.appendingPathComponent("photo.jpg"))
} else {
showLowStorageAlert()
}
volumeAvailableCapacityForImportantUsage:
volumeAvailableCapacityForOpportunisticUsage:
可用于可选操作的空间
用于:缓存、缩略图、预取
阈值较低(系统可能已处于压力之下)
表示“如果你想的话可以继续,但系统快满了”
// ✅ 正确:为不同类型的数据设置不同的阈值 func shouldDownloadThumbnail(size: Int64) -> Bool { let capacity = try? FileManager.default.homeDirectoryForCurrentUser .resourceValues(forKeys: [.volumeAvailableCapacityForOpportunisticUsageKey]) .volumeAvailableCapacityForOpportunisticUsage ?? 0
// 只有在空间充足时才下载可选内容
return size < capacity
}
func canSaveUserDocument(size: Int64) -> Bool { let capacity = try? FileManager.default.homeDirectoryForCurrentUser .resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey]) .volumeAvailableCapacityForImportantUsage ?? 0
// 用户文档是必需的
return size < capacity
}
Caches/ 中的文件会自动从备份中排除,但您应该显式标记其他目录中可重新下载的文件。
// ✅ 正确:将大型可重新下载的文件排除在备份之外
func markExcludedFromBackup(url: URL) throws {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try url.setResourceValues(resourceValues)
}
// 示例:下载的播客剧集
func downloadPodcast(url: URL) throws {
let appSupportURL = FileManager.default.urls(
for: .applicationSupportDirectory,
in: .userDomainMask
)[0]
let podcastURL = appSupportURL
.appendingPathComponent("Podcasts")
.appendingPathComponent(url.lastPathComponent)
// 下载文件
let data = try Data(contentsOf: url)
try data.write(to: podcastURL)
// 标记为从备份中排除(可以重新下载)
try markExcludedFromBackup(url: podcastURL)
}
何时从备份中排除:
// ✅ 检查文件是否从备份中排除
func isExcludedFromBackup(url: URL) -> Bool {
let values = try? url.resourceValues(forKeys: [.isExcludedFromBackupKey])
return values?.isExcludedFromBackup ?? false
}
将文件标记为系统自动清理的候选对象。
// ✅ 正确:将缓存文件标记为可清理
func markAsPurgeable(url: URL) throws {
var resourceValues = URLResourceValues()
resourceValues.isPurgeable = true
try url.setResourceValues(resourceValues)
}
// 示例:缩略图缓存
func cacheThumbnail(image: UIImage, for url: URL) throws {
let cacheURL = FileManager.default.urls(
for: .cachesDirectory,
in: .userDomainMask
)[0]
let thumbnailURL = cacheURL.appendingPathComponent(url.lastPathComponent)
// 保存缩略图
try image.pngData()?.write(to: thumbnailURL)
// 标记为可清理
try markAsPurgeable(url: thumbnailURL)
// 同时从备份中排除
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try thumbnailURL.setResourceValues(resourceValues)
}
注意:Caches/ 中的文件已因其位置而可清理。设置 isPurgeable 是对其他位置文件的建议。
iOS 根据位置清理文件,而非显式的优先级标志。
最先清理(积极):
└── tmp/
- 清理时间:随时(即使应用正在运行)
- 生命周期:数小时到数天
- 用于:真正的临时中间文件
其次清理(存储压力下):
└── Library/Caches/
- 清理时间:当系统需要空间时
- 生命周期:数周到数月(如果空间可用)
- 用于:可重新下载、可重新生成的内容
永不清理(永久):
├── Documents/
│ - 是否备份:✅ 是
│ - 是否清理:❌ 永不(除非应用被删除)
│ - 用于:用户创建的内容
│
└── Library/Application Support/
- 是否备份:✅ 是
- 是否清理:❌ 永不(除非应用被删除)
- 用于:必需的应用程序数据
// ✅ 正确:根据清理优先级需求选择位置
func saveFile(data: Data, priority: FilePriority) throws {
let url: URL
switch priority {
case .essential:
// 永不清理 - 用于用户创建或关键的应用程序数据
url = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0].appendingPathComponent("important.dat")
case .cacheable:
// 在存储压力下清理 - 用于可重新下载的内容
url = FileManager.default.urls(
for: .cachesDirectory,
in: .userDomainMask
)[0].appendingPathComponent("cache.dat")
case .temporary:
// 积极清理 - 用于临时文件
url = FileManager.default.temporaryDirectory
.appendingPathComponent("temp.dat")
}
try data.write(to: url)
// 对于可缓存文件,标记为从备份中排除
if priority == .cacheable {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try url.setResourceValues(resourceValues)
}
}
enum FilePriority {
case essential // 永不清理
case cacheable // 在压力下清理
case temporary // 积极清理
}
// ✅ 正确:监控低存储情况并主动清理
class StorageMonitor {
func checkStorageAndCleanup() {
let homeURL = FileManager.default.homeDirectoryForCurrentUser
guard let values = try? homeURL.resourceValues(forKeys: [
.volumeAvailableCapacityForOpportunisticUsageKey,
.volumeTotalCapacityKey
]) else { return }
let availableSpace = values.volumeAvailableCapacityForOpportunisticUsage ?? 0
let totalSpace = values.volumeTotalCapacity ?? 1
// 计算百分比
let percentAvailable = Double(availableSpace) / Double(totalSpace)
if percentAvailable < 0.10 { // 少于 10% 可用空间
print("⚠️ 检测到存储空间不足,正在清理...")
cleanupCaches()
}
}
func cleanupCaches() {
let cacheURL = FileManager.default.urls(
for: .cachesDirectory,
in: .userDomainMask
)[0]
// 删除旧的缓存文件
let fileManager = FileManager.default
guard let files = try? fileManager.contentsOfDirectory(
at: cacheURL,
includingPropertiesForKeys: [.contentModificationDateKey]
) else { return }
// 按修改日期排序
let sortedFiles = files.sorted { url1, url2 in
let date1 = (try? url1.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
let date2 = (try? url2.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
return (date1 ?? .distantPast) < (date2 ?? .distantPast)
}
// 先删除最旧的文件
for fileURL in sortedFiles.prefix(100) {
try? fileManager.removeItem(at: fileURL)
}
}
}
// ✅ 正确:注册后台任务以清理存储
import BackgroundTasks
func registerBackgroundCleanup() {
BGTaskScheduler.shared.register(
forTaskWithIdentifier: "com.example.app.cleanup",
using: nil
) { task in
self.handleStorageCleanup(task: task as! BGProcessingTask)
}
}
func handleStorageCleanup(task: BGProcessingTask) {
task.expirationHandler = {
task.setTaskCompleted(success: false)
}
// 清理旧缓存
cleanupOldFiles()
task.setTaskCompleted(success: true)
}
// ✅ 正确:获取实际磁盘使用情况(包括文件系统开销)
func getFileSize(url: URL) -> Int64? {
let values = try? url.resourceValues(forKeys: [
.fileAllocatedSizeKey,
.totalFileAllocatedSizeKey
])
// 使用 totalFileAllocatedSize 获取准确的磁盘使用情况
return values?.totalFileAllocatedSize.map { Int64($0) }
}
// ✅ 计算目录大小
func getDirectorySize(url: URL) -> Int64 {
guard let enumerator = FileManager.default.enumerator(
at: url,
includingPropertiesForKeys: [.totalFileAllocatedSizeKey]
) else { return 0 }
var totalSize: Int64 = 0
for case let fileURL as URL in enumerator {
if let size = getFileSize(url: fileURL) {
totalSize += size
}
}
return totalSize
}
// 用法
let cacheSize = getDirectorySize(url: cachesDirectory)
print("缓存使用 \(cacheSize / 1_000_000) MB")
// ✅ 正确:仅在空间可用时下载可选内容
func downloadOptionalContent(url: URL, size: Int64) async throws {
// 检查机会容量
let homeURL = FileManager.default.homeDirectoryForCurrentUser
let values = try homeURL.resourceValues(forKeys: [
.volumeAvailableCapacityForOpportunisticUsageKey
])
guard let available = values.volumeAvailableCapacityForOpportunisticUsage,
size < available else {
print("跳过下载 - 存储空间不足")
return
}
// 继续下载
let data = try await URLSession.shared.data(from: url).0
try data.write(to: cachesDirectory.appendingPathComponent(url.lastPathComponent))
}
// ✅ 正确:在接近存储限制时清理缓存
class CacheManager {
func addToCache(data: Data, key: String) throws {
let cacheURL = getCacheURL(for: key)
// 检查是否应该先清理
if shouldCleanupCache(addingSize: Int64(data.count)) {
cleanupOldestFiles(targetSize: 100 * 1_000_000) // 100 MB
}
try data.write(to: cacheURL)
}
func shouldCleanupCache(addingSize: Int64) -> Bool {
let homeURL = FileManager.default.homeDirectoryForCurrentUser
guard let values = try? homeURL.resourceValues(forKeys: [
.volumeAvailableCapacityForOpportunisticUsageKey
]) else { return false }
let available = values.volumeAvailableCapacityForOpportunisticUsage ?? 0
// 如果可用空间少于 200 MB 则清理
return available < 200 * 1_000_000
}
func cleanupOldestFiles(targetSize: Int64) {
// 删除最旧的缓存文件,直到低于目标大小
// (实现类似于前面的示例)
}
}
// ✅ 正确:下载的播客/视频管理
class MediaDownloader {
func downloadMedia(url: URL) async throws {
let data = try await URLSession.shared.data(from: url).0
// 存储在 Application Support 中(而不是 Caches,以便持久化)
let mediaURL = applicationSupportDirectory
.appendingPathComponent("Downloads")
.appendingPathComponent(url.lastPathComponent)
try data.write(to: mediaURL)
// 但从备份中排除(可以重新下载)
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try mediaURL.setResourceValues(resourceValues)
}
}
// ✅ 检查正在备份的内容
func auditBackupSize() {
let documentsURL = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0]
let size = getDirectorySize(url: documentsURL)
print("Documents(已备份):\(size / 1_000_000) MB")
// 检查应被排除的大型文件
if size > 100 * 1_000_000 { // > 100 MB
print("⚠️ 备份文件过大 - 检查可重新下载的文件")
findLargeFiles(in: documentsURL)
}
}
func findLargeFiles(in directory: URL) {
guard let enumerator = FileManager.default.enumerator(
at: directory,
includingPropertiesForKeys: [.totalFileAllocatedSizeKey]
) else { return }
for case let fileURL as URL in enumerator {
if let size = getFileSize(url: fileURL),
size > 10 * 1_000_000 { // > 10 MB
print("大型文件:\(fileURL.lastPathComponent) (\(size / 1_000_000) MB)")
// 检查是否从备份中排除
if !isExcludedFromBackup(url: fileURL) {
print("⚠️ 此文件是否应从备份中排除?")
}
}
}
}
| 任务 | API | 代码 |
|---|---|---|
| 检查重要文件的空间 | volumeAvailableCapacityForImportantUsageKey | values.volumeAvailableCapacityForImportantUsage |
| 检查缓存的空间 | volumeAvailableCapacityForOpportunisticUsageKey | values.volumeAvailableCapacityForOpportunisticUsage |
| 从备份中排除 | isExcludedFromBackupKey | resourceValues.isExcludedFromBackup = true |
| 标记为可清理 | isPurgeableKey | resourceValues.isPurgeable = true |
| 获取文件大小 | totalFileAllocatedSizeKey | values.totalFileAllocatedSize |
| 清理优先级 | 基于位置 | 使用 tmp/ 或 Caches/ 目录 |
为每个文件设置加密级别。完整指南请参阅 axiom-file-protection-ref。
| 级别 | 何时可访问 | 用于 |
|---|---|---|
.complete | 仅在解锁时 | 密码、令牌、健康数据 |
.completeUnlessOpen | 首次解锁后,如果已打开 | 活动下载、媒体录制 |
.completeUntilFirstUserAuthentication | 首次解锁后(默认) | 大多数应用程序数据 |
.none | 始终,甚至在解锁前 | 后台获取数据、推送有效载荷 |
// 在文件上设置保护
try data.write(to: url, options: .completeFileProtection)
// 在目录上设置保护
try FileManager.default.createDirectory(
at: url,
withIntermediateDirectories: true,
attributes: [.protectionKey: FileProtectionType.complete]
)
// 检查当前保护
let values = try url.resourceValues(forKeys: [.fileProtectionKey])
print("保护级别:\(values.fileProtection ?? .none)")
axiom-storage — 决定文件存储位置axiom-file-protection-ref — 文件加密与安全axiom-storage-diag — 调试存储相关问题最后更新:2025-12-12 技能类型:参考 最低 iOS 版本:5.0(基础功能),11.0(现代容量 API)
每周安装次数
89
代码仓库
GitHub 星标数
601
首次出现
Jan 21, 2026
安全审计
已安装于
opencode74
codex69
claude-code69
gemini-cli67
cursor66
github-copilot64
Purpose : Comprehensive reference for storage pressure, purging policies, disk space, and URL resource values Availability : iOS 5.0+ (basic), iOS 11.0+ (modern capacity APIs) Context : Answer to "Does iOS provide any way to mark files as 'purge as last resort'?"
Use this skill when you need to:
"Does iOS provide any way to mark files as 'purge as last resort'?"
Answer : Not directly, but iOS provides two approaches:
Location-based purging (implicit priority):
tmp/ → Purged aggressively (anytime)Library/Caches/ → Purged under storage pressureDocuments/, Application Support/ → Never purgedCapacity checking (explicit strategy):
volumeAvailableCapacityForImportantUsage — For must-save datavolumeAvailableCapacityForOpportunisticUsage — For nice-to-have data| Resource Key | Type | Purpose | Availability |
|---|---|---|---|
volumeAvailableCapacityKey | Int64 | Total available space | iOS 5.0+ |
volumeAvailableCapacityForImportantUsageKey | Int64 | Space for essential files | iOS 11.0+ |
volumeAvailableCapacityForOpportunisticUsageKey | Int64 | Space for optional files | iOS 11.0+ |
volumeTotalCapacityKey | Int64 | Total volume capacity |
// ✅ CORRECT: Check appropriate capacity before saving
func checkSpaceBeforeSaving(fileSize: Int64, isEssential: Bool) -> Bool {
let homeURL = FileManager.default.homeDirectoryForCurrentUser
do {
let values = try homeURL.resourceValues(forKeys: [
.volumeAvailableCapacityForImportantUsageKey,
.volumeAvailableCapacityForOpportunisticUsageKey
])
if isEssential {
// For must-save data (user-created content, critical app data)
let importantCapacity = values.volumeAvailableCapacityForImportantUsage ?? 0
return fileSize < importantCapacity
} else {
// For nice-to-have data (caches, thumbnails)
let opportunisticCapacity = values.volumeAvailableCapacityForOpportunisticUsage ?? 0
return fileSize < opportunisticCapacity
}
} catch {
print("Error checking capacity: \(error)")
return false
}
}
// Usage
if checkSpaceBeforeSaving(fileSize: imageData.count, isEssential: true) {
try imageData.write(to: documentsURL.appendingPathComponent("photo.jpg"))
} else {
showLowStorageAlert()
}
volumeAvailableCapacityForImportantUsage :
volumeAvailableCapacityForOpportunisticUsage :
Space available for optional operations
Use for: Caches, thumbnails, pre-fetching
Lower threshold (system may already be under pressure)
Indicates "go ahead if you want, but system is getting full"
// ✅ CORRECT: Different thresholds for different data types func shouldDownloadThumbnail(size: Int64) -> Bool { let capacity = try? FileManager.default.homeDirectoryForCurrentUser .resourceValues(forKeys: [.volumeAvailableCapacityForOpportunisticUsageKey]) .volumeAvailableCapacityForOpportunisticUsage ?? 0
// Only download optional content if there's plenty of space
return size < capacity
}
func canSaveUserDocument(size: Int64) -> Bool { let capacity = try? FileManager.default.homeDirectoryForCurrentUser .resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey]) .volumeAvailableCapacityForImportantUsage ?? 0
// User documents are essential
return size < capacity
}
Files in Caches/ are automatically excluded from backup, but you should explicitly mark re-downloadable files in other directories.
// ✅ CORRECT: Exclude large re-downloadable files from backup
func markExcludedFromBackup(url: URL) throws {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try url.setResourceValues(resourceValues)
}
// Example: Downloaded podcast episodes
func downloadPodcast(url: URL) throws {
let appSupportURL = FileManager.default.urls(
for: .applicationSupportDirectory,
in: .userDomainMask
)[0]
let podcastURL = appSupportURL
.appendingPathComponent("Podcasts")
.appendingPathComponent(url.lastPathComponent)
// Download file
let data = try Data(contentsOf: url)
try data.write(to: podcastURL)
// Mark as excluded from backup (can re-download)
try markExcludedFromBackup(url: podcastURL)
}
When to exclude from backup :
// ✅ Check if file is excluded from backup
func isExcludedFromBackup(url: URL) -> Bool {
let values = try? url.resourceValues(forKeys: [.isExcludedFromBackupKey])
return values?.isExcludedFromBackup ?? false
}
Mark files as candidates for automatic purging by the system.
// ✅ CORRECT: Mark cache files as purgeable
func markAsPurgeable(url: URL) throws {
var resourceValues = URLResourceValues()
resourceValues.isPurgeable = true
try url.setResourceValues(resourceValues)
}
// Example: Thumbnail cache
func cacheThumbnail(image: UIImage, for url: URL) throws {
let cacheURL = FileManager.default.urls(
for: .cachesDirectory,
in: .userDomainMask
)[0]
let thumbnailURL = cacheURL.appendingPathComponent(url.lastPathComponent)
// Save thumbnail
try image.pngData()?.write(to: thumbnailURL)
// Mark as purgeable
try markAsPurgeable(url: thumbnailURL)
// Also exclude from backup
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try thumbnailURL.setResourceValues(resourceValues)
}
Note : Files in Caches/ are already purgeable by location. Setting isPurgeable is advisory for files in other locations.
iOS purges files based on location , not explicit priority flags.
PURGED FIRST (Aggressive):
└── tmp/
- Purged: Anytime (even while app running)
- Lifetime: Hours to days
- Use for: Truly temporary intermediates
PURGED SECOND (Storage Pressure):
└── Library/Caches/
- Purged: When system needs space
- Lifetime: Weeks to months (if space available)
- Use for: Re-downloadable, regenerable content
NEVER PURGED (Permanent):
├── Documents/
│ - Backed up: ✅ Yes
│ - Purged: ❌ Never (unless app deleted)
│ - Use for: User-created content
│
└── Library/Application Support/
- Backed up: ✅ Yes
- Purged: ❌ Never (unless app deleted)
- Use for: Essential app data
// ✅ CORRECT: Choose location based on purge priority needs
func saveFile(data: Data, priority: FilePriority) throws {
let url: URL
switch priority {
case .essential:
// Never purged - for user-created or critical app data
url = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0].appendingPathComponent("important.dat")
case .cacheable:
// Purged under storage pressure - for re-downloadable content
url = FileManager.default.urls(
for: .cachesDirectory,
in: .userDomainMask
)[0].appendingPathComponent("cache.dat")
case .temporary:
// Purged aggressively - for temp files
url = FileManager.default.temporaryDirectory
.appendingPathComponent("temp.dat")
}
try data.write(to: url)
// For cacheable files, mark excluded from backup
if priority == .cacheable {
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try url.setResourceValues(resourceValues)
}
}
enum FilePriority {
case essential // Never purge
case cacheable // Purge under pressure
case temporary // Purge aggressively
}
// ✅ CORRECT: Monitor for low storage and clean up proactively
class StorageMonitor {
func checkStorageAndCleanup() {
let homeURL = FileManager.default.homeDirectoryForCurrentUser
guard let values = try? homeURL.resourceValues(forKeys: [
.volumeAvailableCapacityForOpportunisticUsageKey,
.volumeTotalCapacityKey
]) else { return }
let availableSpace = values.volumeAvailableCapacityForOpportunisticUsage ?? 0
let totalSpace = values.volumeTotalCapacity ?? 1
// Calculate percentage
let percentAvailable = Double(availableSpace) / Double(totalSpace)
if percentAvailable < 0.10 { // Less than 10% free
print("⚠️ Low storage detected, cleaning up...")
cleanupCaches()
}
}
func cleanupCaches() {
let cacheURL = FileManager.default.urls(
for: .cachesDirectory,
in: .userDomainMask
)[0]
// Delete old cache files
let fileManager = FileManager.default
guard let files = try? fileManager.contentsOfDirectory(
at: cacheURL,
includingPropertiesForKeys: [.contentModificationDateKey]
) else { return }
// Sort by modification date
let sortedFiles = files.sorted { url1, url2 in
let date1 = (try? url1.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
let date2 = (try? url2.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
return (date1 ?? .distantPast) < (date2 ?? .distantPast)
}
// Delete oldest files first
for fileURL in sortedFiles.prefix(100) {
try? fileManager.removeItem(at: fileURL)
}
}
}
// ✅ CORRECT: Register background task to clean up storage
import BackgroundTasks
func registerBackgroundCleanup() {
BGTaskScheduler.shared.register(
forTaskWithIdentifier: "com.example.app.cleanup",
using: nil
) { task in
self.handleStorageCleanup(task: task as! BGProcessingTask)
}
}
func handleStorageCleanup(task: BGProcessingTask) {
task.expirationHandler = {
task.setTaskCompleted(success: false)
}
// Clean up old caches
cleanupOldFiles()
task.setTaskCompleted(success: true)
}
// ✅ CORRECT: Get actual disk usage (includes filesystem overhead)
func getFileSize(url: URL) -> Int64? {
let values = try? url.resourceValues(forKeys: [
.fileAllocatedSizeKey,
.totalFileAllocatedSizeKey
])
// Use totalFileAllocatedSize for accurate disk usage
return values?.totalFileAllocatedSize.map { Int64($0) }
}
// ✅ Calculate directory size
func getDirectorySize(url: URL) -> Int64 {
guard let enumerator = FileManager.default.enumerator(
at: url,
includingPropertiesForKeys: [.totalFileAllocatedSizeKey]
) else { return 0 }
var totalSize: Int64 = 0
for case let fileURL as URL in enumerator {
if let size = getFileSize(url: fileURL) {
totalSize += size
}
}
return totalSize
}
// Usage
let cacheSize = getDirectorySize(url: cachesDirectory)
print("Cache using \(cacheSize / 1_000_000) MB")
// ✅ CORRECT: Only download optional content if space available
func downloadOptionalContent(url: URL, size: Int64) async throws {
// Check opportunistic capacity
let homeURL = FileManager.default.homeDirectoryForCurrentUser
let values = try homeURL.resourceValues(forKeys: [
.volumeAvailableCapacityForOpportunisticUsageKey
])
guard let available = values.volumeAvailableCapacityForOpportunisticUsage,
size < available else {
print("Skipping download - low storage")
return
}
// Proceed with download
let data = try await URLSession.shared.data(from: url).0
try data.write(to: cachesDirectory.appendingPathComponent(url.lastPathComponent))
}
// ✅ CORRECT: Clean up caches when approaching storage limits
class CacheManager {
func addToCache(data: Data, key: String) throws {
let cacheURL = getCacheURL(for: key)
// Check if we should clean up first
if shouldCleanupCache(addingSize: Int64(data.count)) {
cleanupOldestFiles(targetSize: 100 * 1_000_000) // 100 MB
}
try data.write(to: cacheURL)
}
func shouldCleanupCache(addingSize: Int64) -> Bool {
let homeURL = FileManager.default.homeDirectoryForCurrentUser
guard let values = try? homeURL.resourceValues(forKeys: [
.volumeAvailableCapacityForOpportunisticUsageKey
]) else { return false }
let available = values.volumeAvailableCapacityForOpportunisticUsage ?? 0
// Clean up if less than 200 MB free
return available < 200 * 1_000_000
}
func cleanupOldestFiles(targetSize: Int64) {
// Delete oldest cache files until under target
// (implementation similar to earlier example)
}
}
// ✅ CORRECT: Downloaded podcast/video management
class MediaDownloader {
func downloadMedia(url: URL) async throws {
let data = try await URLSession.shared.data(from: url).0
// Store in Application Support (not Caches, so it persists)
let mediaURL = applicationSupportDirectory
.appendingPathComponent("Downloads")
.appendingPathComponent(url.lastPathComponent)
try data.write(to: mediaURL)
// But exclude from backup (can re-download)
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try mediaURL.setResourceValues(resourceValues)
}
}
// ✅ Check what's being backed up
func auditBackupSize() {
let documentsURL = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
)[0]
let size = getDirectorySize(url: documentsURL)
print("Documents (backed up): \(size / 1_000_000) MB")
// Check for large files that should be excluded
if size > 100 * 1_000_000 { // > 100 MB
print("⚠️ Large backup size - check for re-downloadable files")
findLargeFiles(in: documentsURL)
}
}
func findLargeFiles(in directory: URL) {
guard let enumerator = FileManager.default.enumerator(
at: directory,
includingPropertiesForKeys: [.totalFileAllocatedSizeKey]
) else { return }
for case let fileURL as URL in enumerator {
if let size = getFileSize(url: fileURL),
size > 10 * 1_000_000 { // > 10 MB
print("Large file: \(fileURL.lastPathComponent) (\(size / 1_000_000) MB)")
// Check if excluded from backup
if !isExcludedFromBackup(url: fileURL) {
print("⚠️ Should this be excluded from backup?")
}
}
}
}
| Task | API | Code |
|---|---|---|
| Check space for essential file | volumeAvailableCapacityForImportantUsageKey | values.volumeAvailableCapacityForImportantUsage |
| Check space for cache | volumeAvailableCapacityForOpportunisticUsageKey | values.volumeAvailableCapacityForOpportunisticUsage |
| Exclude from backup | isExcludedFromBackupKey | resourceValues.isExcludedFromBackup = true |
Set encryption level per file. See axiom-file-protection-ref for full guide.
| Level | When Accessible | Use For |
|---|---|---|
.complete | Only while unlocked | Passwords, tokens, health data |
.completeUnlessOpen | After first unlock if already open | Active downloads, media recording |
.completeUntilFirstUserAuthentication | After first unlock (default) | Most app data |
.none | Always, even before unlock | Background fetch data, push payloads |
// Set protection on file
try data.write(to: url, options: .completeFileProtection)
// Set protection on directory
try FileManager.default.createDirectory(
at: url,
withIntermediateDirectories: true,
attributes: [.protectionKey: FileProtectionType.complete]
)
// Check current protection
let values = try url.resourceValues(forKeys: [.fileProtectionKey])
print("Protection: \(values.fileProtection ?? .none)")
axiom-storage — Decide where to store filesaxiom-file-protection-ref — File encryption and securityaxiom-storage-diag — Debug storage-related issuesLast Updated : 2025-12-12 Skill Type : Reference Minimum iOS : 5.0 (basic), 11.0 (modern capacity APIs)
Weekly Installs
89
Repository
GitHub Stars
601
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode74
codex69
claude-code69
gemini-cli67
cursor66
github-copilot64
Clerk Swift iOS 原生集成指南:SwiftUI/UIKit 认证实现与快速开始
904 周安装
Sentry 错误追踪指南:Node.js/Python 异常监控、性能分析与最佳实践
162 周安装
iOS TestFlight崩溃调查指南:使用Xcode Organizer快速定位与修复Beta测试问题
162 周安装
iOS小组件与扩展开发指南:解决数据更新、实时活动与控制中心问题
161 周安装
会话管理最佳实践:JWT令牌、CSRF防护、Redis存储与安全实现指南
161 周安装
AI智能体上下文管理技能context-surfing:防止漂移与幻觉,实现高保真执行
168 周安装
AI安全文档生成工具 | 一键创建合规策略、安全指南与最佳实践
162 周安装
| iOS 5.0+ |
isExcludedFromBackupKey | Bool | Exclude from iCloud/iTunes backup | iOS 5.1+ |
isPurgeableKey | Bool | System can delete under pressure | iOS 9.0+ |
fileAllocatedSizeKey | Int64 | Actual disk space used | iOS 5.0+ |
totalFileAllocatedSizeKey | Int64 | Total allocated (including metadata) | iOS 5.0+ |
| Mark purgeable | isPurgeableKey | resourceValues.isPurgeable = true |
| Get file size | totalFileAllocatedSizeKey | values.totalFileAllocatedSize |
| Purge priority | Location-based | Use tmp/ or Caches/ directory |