如何找到我的 Core Data 持久存储的大小和文件系统上的可用空间? [英] How do I find the size of my Core Data persistent store and the free space on the file system?

查看:13
本文介绍了如何找到我的 Core Data 持久存储的大小和文件系统上的可用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Core Data 框架开发数据库应用程序.在这个应用程序中,我需要显示应用程序当前在 iPhone 上使用了多少数据.有没有办法做到这一点?

I am working on a database application using the Core Data framework. In this application I need to display how much data the application currently is using on the iPhone. Is there any way to do this?

推荐答案

我发现 Apple Dev Forums 上的这个答案 可用于查找应用程序主目录分区上的可用磁盘空间(注意目前每个设备上有两个分区).

I found this answer on the Apple Dev Forums to be useful for finding disk space available on the apps' home directory partition (note there are currently two partitions on each device).

使用 NSPersistentStoreCoordinator 获取您的商店集合.

Use NSPersistentStoreCoordinator to get your store collection.

使用 NSFileManager 获取每个 store 的字节大小 (unsigned long long)

Use NSFileManager to get each stores' size in bytes (unsigned long long)

NSArray *allStores = [self.persistentStoreCoordinator persistentStores];
unsigned long long totalBytes = 0;
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSPersistentStore *store in allStores) {
    if (![store.URL isFileURL]) continue; // only file URLs are compatible with NSFileManager
    NSString *path = [[store URL] path];
    DebugLog(@"persistent store path: %@",path);
    // NSDictionary has a category to assist with NSFileManager attributes
    totalBytes += [[fileManager attributesOfItemAtPath:path error:NULL] fileSize];
}

请注意,上面的代码是在我的应用程序委托的一个方法中,它有一个属性 persistentStoreCoordinator.

Note that the code above is in a method of my app delegate, and it has a property persistentStoreCoordinator.

这篇关于如何找到我的 Core Data 持久存储的大小和文件系统上的可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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