views:

120

answers:

2

I'm diving into iPhone development, so I apologize in advance if this is a ridiculous question, but in a new iPad app project using the Core Data framework, here's the generated code for creating the persistentStoreCoordinator...

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"ApplicationName.sqlite"]];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

     Typical reasons for an error here include:
     * The persistent store is not accessible
     * The schema for the persistent store is incompatible with current managed object model
     Check the error message to determine what the actual problem was.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return persistentStoreCoordinator;

}

My questions are...

  1. The first time I run the app, is the ApplicationName.sqllite database created automatically if it doesn't exist? If not, when is it created? When data is added to it programmatically?
  2. Once the DB does exist, where can I locate the file? I'd like to open it with a different program so I can manually manipulate the data.

Thanks so much in advance for your help! I'm going to continue researching these questions right now.

+2  A: 

Yes, the ApplicationName.sqlite database will be created if none exists already. It will be located within your application's Documents directory (assuming that's what your -applicationDocumentsDirectory method returns).

If you are running this in the Simulator, you can generally find application data within ~/Library/Application Support/iPhone Simulator. On the 3.2+ SDK, different OS versions are used to group applications built using a particular SDK, so you'll need to go into the appropriate version directory, then Applications, then find the application's numerically coded directory (pretty much by trial-and-error). The Documents directory, and the sqlite database, will be found there.

If running on the device, you can go to the Organizer, select your device (while connected to the system), and go to the Applications grouping in the Summary tab. Click the disclosure arrow next to your application's name and you can drag out the Application Data from there into a directory to pull all of your application's working data out of the application on the device.

Brad Larson
Thanks, Brad, you've been a huge help all day!
BeachRunnerJoe
+1  A: 

The store is created by the

addPersistentStoreWithType:configuration:URL:options:error:

method. Use

NSLog(@"store path: %@", storeUrl);

to see the file path of the store in the applicationDocumentsDirectory.

To download and browse the whole applicationDocumentsDirectory of your device, use the Xcode Organizer: choose your device, click on the disclosure triangle of your application and then on the arrow on the right of the application bundle.

IlDan