Reputation: 6068
I created a page based application which does not support core data by default (the option does not show when creating the project). So, what I did was create a second project of whatever type that does support core data and copy&past the AppDelegate code for the core data support. Here it is:
func saveContext () {
var error: NSError? = nil
let managedObjectContext = self.managedObjectContext
if managedObjectContext != nil {
if managedObjectContext.hasChanges && !managedObjectContext.save(&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.
//println("Unresolved error \(error), \(error.userInfo)")
abort()
}
}
}
// #pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
var managedObjectContext: NSManagedObjectContext {
if !_managedObjectContext {
let coordinator = self.persistentStoreCoordinator
if coordinator != nil {
_managedObjectContext = NSManagedObjectContext()
_managedObjectContext!.persistentStoreCoordinator = coordinator
}
}
return _managedObjectContext!
}
var _managedObjectContext: NSManagedObjectContext? = nil
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
var managedObjectModel: NSManagedObjectModel {
if !_managedObjectModel {
let modelURL = NSBundle.mainBundle().URLForResource("Wink", withExtension: "momd")
_managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)
}
return _managedObjectModel!
}
var _managedObjectModel: NSManagedObjectModel? = nil
// 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.
var persistentStoreCoordinator: NSPersistentStoreCoordinator {
if !_persistentStoreCoordinator {
let storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Wink.sqlite")
var error: NSError? = nil
_persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
if _persistentStoreCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error) == nil {
/*
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: true, NSInferMappingModelAutomaticallyOption: true}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
//println("Unresolved error \(error), \(error.userInfo)")
abort();
}
}
return _persistentStoreCoordinator!
}
var _persistentStoreCoordinator: NSPersistentStoreCoordinator? = nil
I have made no changes to it.
Now I want to instantiate a NSManagedObject:
var appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate;
var entity = NSEntityDescription.entityForName(ProfileDataEntityIdentifier, inManagedObjectContext: appDelegate.managedObjectContext);
var profileData = ProfileData(entity: entity, insertIntoManagedObjectContext: appDelegate.managedObjectContext);
Which, of course, calls the AppDelegate methods, which in turn raise an exception:
fatal error: unexpectedly found nil while unwrapping an Optional value
The exception is raised in the managedObjectModel getter method, on the return statement. Inspecting the code, it seems to me that copy&pasting the code is not enough, because it seems to be missing some resource (that URL points somewhere). So how do I create this resource? Do I need do anything else to add Core Data support?
Upvotes: 0
Views: 1425
Reputation: 70966
Copying the code is not enough. Core Data requires a data model that tells it how the data is configured. It's basically the Core Data equivalent of a SQL schema, except that with Core Data it goes in a separate file. That's what the URL refers to-- it's trying to find a file named Wink.mom
that represents the model. But if you don't have a model file in your project, that file doesn't exist.
You need to add a file to the project that corresponds to this URL. Use Xcode's new-file wizard to add a Core Data model file. Give it a name that matches the URL you're trying to load. Then edit the data model file to create some entity types, so that you have a schema to work with.
Upvotes: 1