如何查找我的Core Data持久存储的大小和文件系统上的可用空间? [英] How do I find the size of my Core Data persistent store and the free space on the file system?
问题描述
我正在使用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?
推荐答案
我发现这个答案在苹果开发论坛用于查找应用程序主目录分区上可用的磁盘空间(注意每个设备上目前有两个分区)。
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
字节(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屋!