BinaryMisfit
BinaryMisfit

Reputation: 30589

Using Core Data to fetch generates 'unrecognized selector error'?

I'm trying to retrieve a list of objects saved using Core Data. No changes where made to the default setup made by Xcode when creating the project. There are items in the actual data store, and the entity Transaction works fine when saving but when running the following code:

    NSManagedObjectContext * context = [[NSApp delegate] managedObjectModel];
    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription * entity = [NSEntityDescription 
                                    entityForName:@"Transaction" 
                                    inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError * error = nil;
    NSArray * transactionList = [context executeFetchRequest:fetchRequest error:&error];
    if (&error != nil) {
        [Utility showMessage:error.description asError:YES];
    } else {
        [Utility showMessage:[NSString stringWithFormat: @"Items: %@", transactionList.count] asError:NO];
    }    

I receive the following error when trying to create the entity object.

[NSManagedObjectModel persistentStoreCoordinator]: unrecognized selector sent to instance

What am I missing, or what do I do to check what is causing the error?

Footnotes
- Utility is a static class which simply generates a NSAlert box.
- I've been using this tutorial to try and understand how the code works

Upvotes: 2

Views: 1250

Answers (2)

Mundi
Mundi

Reputation: 80271

From your code, it is not clear what exactly you are assigning to managed object context. It should be a managed object context, not a managed object model.

Also, you should check if (error!=nil) not &error. Read up on your C pointer syntax (;-).

Upvotes: 2

Maurice Kelly
Maurice Kelly

Reputation: 1472

In the first line you're fetching the managedObjectModel from your app delegate and assigning it to an NSManagedObjectContext. You should fetch the managedObjectContext instead.

Upvotes: 2

Related Questions