3
votes

I created a storyboard with a UITableViewController, then added a Core Data entity. The application at this point built and run without errors, but the UITableViewController was showing no data.

I deleted the TVC and rebuilt in StoryBoard, but ever since I'm getting an error when I run the application and try to open the TVC:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Attractions''

With a bit of research, realize that this is due to my managedObjectContext being empty, but for the life of me I cannot figure out WHY it's empty.

In the TVC header file:

#import <UIKit/UIKit.h>
#import "Attractions.h"
#import "AttractionListViewCell.h"
#import "ApplicationNameAppDelegate.h"

@interface AttractionListViewController : UITableViewController
{
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *AttractionsArray;    
}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *AttractionsArray;

- (void) fetchrecords;

@end

In the TVC model file:

ApplicationNameAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];

NSLog(managedObjectContext);
// Create connection to the DB via Context
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Attractions" inManagedObjectContext:managedObjectContext];    

In the ApplicationNameAppDelegate.h file:

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

Any help or insight you could provide would be much appreciated.

EDIT - Added AppDelegate info:

#import <UIKit/UIKit.h>
#import "AttractionListViewController.h"
#import <CoreData/CoreData.h>

@class AttractionListViewController;

@interface AppNameAppDelegate : UIResponder <UIApplicationDelegate>
{
    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;
    NSPersistentStoreCoordinator *persistentStoreCoordinator;
}

@property (strong, nonatomic) AttractionListViewController *viewController;
@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end
3
Use this NSLog(@"%@", managedObjectContext); instead of NSLog(managedObjectContext); ans say what you see in the console. Then, post the implementation of Core Data stack within your AppDelegate. - Lorenzo B
Here's the log '2012-11-05 08:27:13.304 [9872:11603] (null)' - user1797508

3 Answers

1
votes

This line:

NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];

You are declaring a local managedObjectContext and assigning it rather than what you should do:

managedObjectContext = [appDelegate managedObjectContext];

which will use the TVC's iVar

0
votes

So I found what the issue was. I hadn't copied over all the classes from the AppDelegate in the xcode generated example, so the classes that defined the managedObject and persistent store etc weren't there.

-1
votes

In your View Controller implementation file, right under this bit of code:

- (void)viewDidLoad
{

add this bit of code:

id delegate = [[UIApplication sharedApplication] delegate]; self.managedObjectContext = [delegate managedObjectContext];