2

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

BinaryMisfit
  • 29,219
  • 2
  • 37
  • 44

2 Answers2

2

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.

Maurice Kelly
  • 1,462
  • 12
  • 15
2

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 (;-).

Mundi
  • 79,884
  • 17
  • 117
  • 140