0
votes

I've been trying to learn how to use CoreData on iOS 7. While working with the Data Modeler, somehow, Xcode managed to create some default template methods like:

// Returns the persistent store coordinator for the application. // If the coordinator doesn't already exist, it is created and the application's store added to it. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator; }

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Myapp_2.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.

     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.


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into

the application's resources directory instead of a writeable directory.

     If you encounter schema incompatibility errors during development, you can reduce their frequency by:
     * Simply deleting the existing store:
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
     @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration

Programming Guide" for details.

     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return _persistentStoreCoordinator; }

I don't know exactly how it managed to generate this obviously very useful starting point, and, I have not been able to force Xcode to regenerate the template Core Data functions in AppDelegate for other projects.

So my question is - how do we force Xcode 5 to regenerate this code? I don't see anything obvious.

Thank you.

2
i guess you have to create a new project and copy these methods from there...Volker
I didn't have to do anything like that, it generated the code in the app delegate automatically, and it even added stuff to my app delegate's .h file..geekyaleks
yes it did when you created a new project with using CoreData checkedVolker
Ohh, I see it now.. I guess, there is no way to force it to recreate anything, because it is created at project creation time. It is not something that can be recreated over and over again..geekyaleks

2 Answers

2
votes

When you create a new project, you should check "Use Core Data" checkbox.

If you want to add core data to an existing project, you can follow this tutorial http://wiresareobsolete.com/wordpress/2009/12/adding-core-data-existing-iphone-projects/

EDIT : You have a lot of articles about that : https://www.google.fr/search?q=add+core+data+to+existing+project+xcode+5&oq=add+coredata+to+&aqs=chrome.1.69i57j0l5.2540j0j7&sourceid=chrome&espv=210&es_sm=91&ie=UTF-8

1
votes

Create a new project, e.g. The master-detail template. Them select "Use Core Data".