0

n core data, I create the model and then generate an NSManagedObjectContext subclass but my entity table never gets created in the SQLlite database. Do I have to do this by hand?

My app crashes on the main thread on this line:

// Save the data in core data
   UsersTest *coreDataUser =[NSEntityDescription insertNewObjectForEntityForName:@"UsersTest" inManagedObjectContext:self.managedObjectContext];

The UsersTest entity was create in XCode and I add the UsersTest.h to my controller.

What gives? Is this a bug? And I assert on the context that we are in the main thread too.

I get this exception:

NSException *    name:@"NSInternalInconsistencyException" reason:@"+entityForName: could not locate an entity named 'UsersTest' in this model."    0x0895ee20

Do I have to open the database or anything (followed this link: http://www.codigator.com/tutorials/ios-core-data-tutorial-with-example/)

Thanks, and really stuck.

My appDelegate.m is like this:

// 1
- (NSManagedObjectContext *) managedObjectContext {
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator: coordinator];
    }

    return _managedObjectContext;
}

//2
- (NSManagedObjectModel *)managedObjectModel {
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

    return _managedObjectModel;
}

//3
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
                                               stringByAppendingPathComponent: @"PhoneBook.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];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
cdub
  • 24,555
  • 57
  • 174
  • 303
  • Can you show your model with an entity called UsersTest? – Aaron Brager Nov 28 '13 at 03:13
  • Here's the model http://screencast.com/t/mPJzplcMgR – cdub Nov 28 '13 at 03:18
  • So stuck on this for a couple of days – cdub Nov 28 '13 at 03:18
  • Is the xcdatamodeld file added to the correct Xcode target? – Aaron Brager Nov 28 '13 at 03:21
  • how do you check that – cdub Nov 28 '13 at 03:22
  • Look at the Target Membership option in the right pane. – Aaron Brager Nov 28 '13 at 03:25
  • the entity never gets created in the sqlite db ata ll, i have no idea why, i just changed the target to ios 7 – cdub Nov 28 '13 at 03:36
  • Look at the screenshot here to see what target membership looks like: http://stackoverflow.com/q/13571080/1445366 – Aaron Brager Nov 28 '13 at 03:39
  • cool it got passed the exception, but I don't see the table in my sqlite db – cdub Nov 28 '13 at 03:52
  • Did you inspect the NSError object vended by the `save` method? – Aaron Brager Nov 28 '13 at 03:54
  • @chris, "I don't see the table in my sqlite db." Are you opening the sqlite file from ~/Library/Application Support/iPhone Simulator//Applications/? If so and you are not seeing the _table_ then you may have an issue with your persistent store coordinator. – Chris Nov 28 '13 at 04:01
  • Yes the error I'm getting now is no persistence stores. The Target Membership was the first problem, now this one – cdub Nov 28 '13 at 04:11
  • 1
    No persistent store means no sqlite database. Checkout my answer below. Creating a new project as a Master-Detail Application will show how to configure Core Data in your AppDelegate. – Chris Nov 28 '13 at 04:12
  • Would you mind showing how you are initializing you NSPersistentStoreCoordinator and NSManagedObjectModel in your project? – Chris Nov 28 '13 at 04:13
  • okay i'll give that a try – cdub Nov 28 '13 at 04:13
  • Just as a test, I created a new project as a Master-Detail Application, added an Entity named "UsersTest" with an attribute named "testing" and was able to insert the entity into the managed object context using your statement `UsersTest *coreDataUser =[NSEntityDescription insertNewObjectForEntityForName:@"UsersTest" inManagedObjectContext:self.managedObjectContext];` It shows up in the sqlite file. – Chris Nov 28 '13 at 04:17
  • Hmm I added the appDelegate.m – cdub Nov 28 '13 at 04:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42085/discussion-between-chris-and-chris) – Chris Nov 28 '13 at 04:19
  • hmmm I still get no persistentstore and its not null – cdub Nov 28 '13 at 04:31

3 Answers3

0

Make sure you are capitalising the name exactly as it appears in the core data model. You may also want to check if the managedObjectContext is nil.

Ben Avery
  • 1,724
  • 1
  • 20
  • 33
0

Before doing anything, you may want to create a new project that is based on a Master-Detail Application. It will configure all of the required components of Core Data and set them up in the AppDelegate.m file.

At the very least create, a Master-Detail Application project and make sure the AppDelegate in your project is configuring Core Data the same way as the Master-Detail Application project. It is a good way to get started with Core Data.

otherwise take the time to read:

https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/cdProgrammingGuide.html

It can be sometimes difficult to debug Core Data. Apple's documentation on Core Data is thorough to say the least. You may find where you are going wrong.

Edit 1:

Make sure your NSManagedObjectContext is not nil. If so, the you will also get a persistent store coordinator not found error.

Chris
  • 1,663
  • 1
  • 15
  • 19
  • Is a master detail application have core data in it our do I need to set all that up again just like in my app? – cdub Nov 28 '13 at 04:34
  • master detail applications set up Core Data in a ready-to-use state. There is no more configuring required if it is a brand new project. Your managedObjectModel seems to be initializing itself using bundle merging. Just to ensure its not the model, try replacing `_managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];` With `NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"teste" withExtension:@"momd"]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];` and relace "teste" with the name of your model (e.g. teste.xcdatamodeld). – Chris Nov 28 '13 at 13:06
0

Your current stack looks fine. The on disk sqlite file is probably invalid if you have changed your Model at any point.

Make sure you are doing a full clean of your project (Cmd-Shift-Alt-K) AND doing a reset of your simulator (iOS Simulator > Reset Content And Settings…) for each edit of your CoreData model.

Each time you change your model the existing sqlite file on disk will become incompatible with the model and thus become unreadable AND unwritable.

CoreData files are upgradable but only in certain ways and when doing initial dev the easiest way to upgrade is to frag everything and start again.

Community
  • 1
  • 1
Warren Burton
  • 17,451
  • 3
  • 53
  • 73