First off, I'll make mention that I've seen lots of answers to this type of question on this site and others, but I can't seem to relate those solutions to my problem. I do apologize, however, if the answer I'm looking for already exists or turns out to be incredibly simple (although I'm definitely hoping that's the case).
I'm attempting to add Core Data functionality to my existing application and I've taken the following steps:
a. Added the Core Data framework and CoreData imports to the relevant files
b. Added a Data Model file to my Resources folder named "MyProject.xcdatamodeld" and added entities/attributes to the data model. I've also generated a class for each entity.
c. Added the following properties/method to my AppDelegate.h:
@property (readonly, strong, nonatomic) NSManagedObjectModel* managedObjectModel;
@property (readonly, strong, nonatomic) NSManagedObjectContext* managedObjectContext;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator* persistentStoreCoordinator;
-(NSString*)applicationDocumentsDirectory;
d. Added the following code to my AppDelegate.m:
@synthesize managedObjectModel = __managedObjectModel;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
-(NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
-(NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
__managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return __managedObjectModel;
}
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyProject.sqlite"]];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if(![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error])
{
/*Error for store creation should be handled in here*/
}
return __persistentStoreCoordinator;
}
-(NSString *)applicationDocumentsDirectory
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
The problem is, when this line executes:
__managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
I'm getting an NSInvalidArgumentException saying a nil object is being inserted into an NSArray.
I've tried replacing that code with the following:
NSURL* modelURL = [[NSBundle mainBundle] URLForResource:@"MyProject" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
and with that I'm not getting any exceptions, but after executing those lines __managedObjectModel
is still null.
I can't seem to figure out what's wrong. Any ideas?
(As a side note, I'm using ARC which is why you don't see any calls to retain).
Any help/suggestions are much appreciated.
Thanks,
B.J.