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