Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the size of my Core Data persistent store and the free space on the file system?

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?

like image 959
Nic Avatar asked Nov 12 '09 04:11

Nic


People also ask

Where is Core Data data stored?

The persistent store should be located in the AppData > Library > Application Support directory. In this example you should see a SQLite database with extension . sqlite. It is possible that you don't see the persistent store in the Application Support directory.

What is persistent store in Core Data?

A persistent store is the interface between the coordinator and the permanent state of the object graph for both reading and writing. When a change is pushed onto the store, the change becomes a permanent part of the object state, meaning the position of the storage in memory or on disk is not relevant.

Where is Core Data SQLite file?

Yes, there is always a SQLite behind it. You can find it in the documents folder. As with every new build a new documents folder is created on the simulator it's getting quite cumbersome to search it manually everytime. Create a function in your app and print it out.

How can you retrieve data from Core Data?

Fetching Data From CoreData We have created a function fetch() whose return type is array of College(Entity). For fetching the data we just write context. fetch and pass fetchRequest that will generate an exception so we handle it by writing try catch, so we fetched our all the data from CoreData.


1 Answers

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).

Use NSPersistentStoreCoordinator to get your store collection.

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];
}

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

like image 191
ohhorob Avatar answered Oct 08 '22 00:10

ohhorob