Search code examples
iphonexcodecore-dataxcode4xcode-template

Core Data Project template questions in XCode 4


I've created a Window based iPhone application in XCode 4 with Core Data. Regarding the core data bits I have some questions:

  1. In the application delegate header file there are no the 3 core data properties do not appear as instance variables? i.e. there are no variables listed in the @interface section, yet there are properties for them and they are synthesized in implementation file. Is this correct?

  2. In the default persistance mechanism SQLite? I see in the "persistentStoreCoordinator" method that the storeURL is "...URLByAppendingPathComponent:@"CoreDataProjectTemplate.sqlite"

  3. Where & when will the actual sqlite persistance file get created? I can't see in the code from the template where this would be? Do you have to add your own code to create this?


Solution

  • 1 I assume you are referring to the following:

    @synthesize managedObjectContext=__managedObjectContext;
    @synthesize managedObjectModel=__managedObjectModel;
    @synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
    

    This format allows you to create accessors for a variable of a different name (i.e. the getter / setter accessor names can be different from the variable name). If the variable has not been previously defined then the synthesize operation will automatically create a synthesized instance variable for you.

    2 As you've inferred from the filename, the default persistent store for CoreData is SQLite; however it's not limited to this one type. When creating your persistent store for the first time you send a message to the persistentStoreCoordinator in which you set the addPersistentStoreWithType to one of the following:

    NSSQLiteStoreType
    NSBinaryStoreType
    NSInMemoryStoreType
    

    To be honest, unless you have a good reason to change it you're probably best just sticking with SQLite.

    3 The location URL of the persistent store is built by establishing the directory that the application is executing within (with a message to applicationDocumentsDirectory - written elsewhere in your code) then appending the filename of the persistent store to it via the URLByAppendingPathComponent parameter. You can modify this to add sub-folders or changing the filename if you wish.