“NSManagedObject”类的 NSManagedObject 必须具有有效的 NSEntityDescription

Posted

技术标签:

【中文标题】“NSManagedObject”类的 NSManagedObject 必须具有有效的 NSEntityDescription【英文标题】:An NSManagedObject of class 'NSManagedObject' must have a valid NSEntityDescription 【发布时间】:2017-05-31 09:13:04 【问题描述】:

我正在使用 CoreData 处理 Objective-C,我遇到了错误...

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“类的 NSManagedObject 'NSManagedObject' 必须有一个有效的 NSEntityDescription。'

我将CoreData 添加到我的项目中。我在viewControllerA 中创建了saveData 方法,并在viewControllerB 中调用了它。

在这种情况下,我遇到了错误,如果我在 viewDidLoadviewControllerA 中调用 saveData 方法,它可以正常工作。

In AppDelegate.h

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

In AppDelegate.m

#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory 
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.brninfotech._607_introToCoreDataFinal" in the application's documents directory.
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];


- (NSManagedObjectModel *)managedObjectModel 
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) 
    return _managedObjectModel;

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"VehicleNumberDataBase" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) 
    return _persistentStoreCoordinator;


// Create the coordinator and store

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"VehicleNumberDataBase.sqlite"];


//    NSLog(@"StoreURL is %@",storeURL);
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 
    // Report any error we got.
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
    dict[NSLocalizedFailureReasonErrorKey] = failureReason;
    dict[NSUnderlyingErrorKey] = error;
    error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
    // Replace this with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();


return _persistentStoreCoordinator;



- (NSManagedObjectContext *)managedObjectContext 
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) 
    return _managedObjectContext;


NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) 
    return nil;

_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;


#pragma mark - Core Data Saving support

- (void)saveContext 
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) 
    NSError *error = nil;
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) 
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    




in viewControllerA viewDidLoad 

self.ad = (AppDelegate *)[[UIApplication sharedApplication]delegate];
self.VehicleNumberED = [NSEntityDescription entityForName:@"VehicleNumberEntity" inManagedObjectContext:self.ad.managedObjectContext];

- (void)saveData 

NSManagedObject * managedObj = [[NSManagedObject alloc]initWithEntity:self.VehicleNumberED insertIntoManagedObjectContext:self.ad.managedObjectContext];

[managedObj setValue:self.textFieldString forKey:@"vehicleNumberAtt"];

NSError * errorObj;
[self.ad.managedObjectContext save:&errorObj];

if (errorObj) 
    NSLog(@"Something goes wrong");
else 
    NSLog(@"Saved Successfully");


NSFetchRequest * fetReq = [NSFetchRequest fetchRequestWithEntityName:@"VehicleNumberEntity"];

NSError * fetchErrorObj;

self.storedDataArray = [self.ad.managedObjectContext executeFetchRequest:fetReq error:&fetchErrorObj];


NSLog(@"array count is %lu", self.storedDataArray.count);

for (int i=0; i<self.storedDataArray.count; i++) 

    self.storedManagedObj = [self.storedDataArray objectAtIndex:i];

    self.vehicleNumberArray = [self.storedManagedObj valueForKey:@"vehicleNumberAtt"];



NSLog(@"Vehicle number is : %@", [self.storedManagedObj valueForKey:@"vehicleNumberAtt"]);



in ViewVontrollerB 

- (IBAction)saveVehicleNumberButton:(UIButton *)sender 

VehicleDetailsViewController *vedvc = [[VehicleDetailsViewController alloc]init];
vedvc = [self.storyboard instantiateViewControllerWithIdentifier:@"VeDVC"];
vedvc.textFieldString = self.vehicleNumberTextField.text;
[vedvc saveData];

[self.navigationController pushViewController:vedvc animated:YES];

【问题讨论】:

【参考方案1】:

这里的问题是您正在执行[vedvc saveData] 之前 显示vedvc 视图控制器。此时vedvc还没有显示出来,所以它的viewDidLoad还没有被执行。因此self.adself.VehicleNumberED 都为零。因此错误:实体描述为零。

最简单的解决方法是将这两个变量(adVehicleNumberED)的初始化移动到saveData 方法。但是你最好重新考虑一下你的代码结构。

【讨论】:

以上是关于“NSManagedObject”类的 NSManagedObject 必须具有有效的 NSEntityDescription的主要内容,如果未能解决你的问题,请参考以下文章

原因:'一个'NSManagedObject'类的NSManagedObject必须有一个有效的NSEntityDescription? [复制]

“NSManagedObject”类的 NSManagedObject 必须具有有效的 NSEntityDescription

创建 NSManagedObject 派生类的问题

需要继承 NSManagedObject 的自定义类的子类

Core Data中设置实体间关系的原因

如何以可在绑定中访问的可重用方式修改 NSManagedObject 类的行为?