处理 managedObjectContext 的核心数据错误

Posted

技术标签:

【中文标题】处理 managedObjectContext 的核心数据错误【英文标题】:Core data error dealing with managedObjectContext 【发布时间】:2012-12-25 08:31:07 【问题描述】:

下面是我遇到错误的代码行。错误也在下面。我理解为什么会出现错误,因为 managedObjectContext 为零。我不知道如何解决这个问题。

NSEntityDescription *entity = [NSEntityDescription
                                       entityForName:@"Customer" inManagedObjectContext:managedObjectContext];

错误

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

这里有一些更相关的代码。

- (NSManagedObjectContext *)managedObjectContext

    if (_managedObjectContext != nil) 
        return _managedObjectContext;
    

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) 
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    
    return _managedObjectContext;

- (NSManagedObjectModel *)managedObjectModel

    if (_managedObjectModel != nil) 
        return _managedObjectModel;
    
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Customer" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;


// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

    if (_persistentStoreCoordinator != nil) 
        return _persistentStoreCoordinator;
    

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"rolo.sqlite"];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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.

         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.


         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
         @NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    

    return _persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory

    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

【问题讨论】:

您有 Customer.m 和 .h 文件吗? @MichaelDautermann 是的,我确实有 .h 和 .m 文件 你的数据模型是客户吗? 看这里***.com/questions/6706505/… 我的数据模型叫做客户。我去了那个链接并尝试了它所说的但它没有用。我查看了这个网站上的多个问题,但没有一个有效。这就是我再次提出问题的原因。 【参考方案1】:

您没有向我们展示您如何分配您使用的variable managedObjectContext

我想你无论如何都不想这样做:)

=> 您可能想要 self.managedObjectContext 属性

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Customer" inManagedObjectContext:self.managedObjectContext];

【讨论】:

即使我将 managedObjectContext 更改为 self.managedObjectContext 我仍然得到同样的错误。

以上是关于处理 managedObjectContext 的核心数据错误的主要内容,如果未能解决你的问题,请参考以下文章

NSManagedObject 总是在同一个 ManagedObjectContext 上创建

尝试从 iOS 中的 Singleton 类初始化 ManagedObjectContext

self.managedObjectContext 和 managedObjectInstance.managedObjectContext 有啥区别?

单例 managedObjectContext

多个 ManagedObjectContext

AppDelegate 类型的值没有成员 managedObjectContext