iOS - CoreData - TableViewController - NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext

StackOverflow https://stackoverflow.com/questions/13215962

  •  30-07-2021
  •  | 
  •  

Вопрос

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

Нет правильного решения

Другие советы

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

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.

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];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top