ios - 实体在我的视图控制器中是nil但不是app delegate

ios - 实体在我的视图控制器中是nil但不是app delegate,第1张

我想在单个视图应用程序中使用CoreData(没有表视图控制器)所以我从主/详细应用程序中获取了核心数据代码并将其放在我的单个视图应用程序的AppDelegate中,导入核心数据等。在app委托中的application:didFinishLaunchingWithOptions方法中,我能够获取核心数据,插入和检索数据等。但是,当我尝试在主视图控制器中执行获取请求时,我得到这个错误:

'NSInvalidArgumentException', reason: ' entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Dumbquiz''

在这个视图控制器中,我导入了CoreData框架,在头文件中为托管对象上下文创建了一个属性,如下所示

@property (nonatomic,strong) NSManagedObjectContext* managedObjectContext;

然后合成它

@synthesize managedObjectContext;

在视图中加载,这是我的代码,我尝试检索我在application:didFinishLaunchingWithOptions中成功获取的相同数据

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Dumbquiz" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSError *error;
    self.quizData = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    NSLog(@"quizdata %@", self.quizData);

你能解释一下为什么我会收到这个错误

'NSInvalidArgumentException', reason: ' entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Dumbquiz''

在视图控制器中,如果我能够在app委托文件中获取数据?

应用程序中的

代码:didFinishLaunchingWithOptions

NSManagedObjectContext *context = [self managedObjectContext];
    Quoraquiz *failedBankInfo = [NSEntityDescription
                                       insertNewObjectForEntityForName:@"Dumbquiz"
                                       inManagedObjectContext:context];
    [failedBankInfo setValue:@"do you like big cupcakes" forKey:@"question"];
    [failedBankInfo setValue:@"yes" forKey:@"answer1"];
    [failedBankInfo setValue:@"no" forKey:@"answer2"];
    [failedBankInfo setValue:@"maybe" forKey:@"answer3"];
    [failedBankInfo setValue:@"maybe" forKey:@"correctAnswer"];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Dumbquiz" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (Quoraquiz *info in fetchedObjects) {
        NSLog(@"question: %@", [info valueForKey:@"question"]);

    }

最佳答案:

2 个答案:

答案 0 :(得分:0)

将此代码添加到我的viewController修复了问题

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

答案 1 :(得分:0)

NSManagedObjectContext保存商店的某些状态的上下文(即您的核心数据信息)以及所有更改。每次调用save: moc(“托管对象 - 上下文”)时,都会将上下文中的所有更改保存到核心数据(基本上是您的SQLite)。当您调用{{1删除所有更改 - 只保留一个干净的reset:而不做任何更改。 您需要始终使用相同的moc,并使用moc处理所有核心数据调用。

要从您的app appate获取moc对象:

moc

或者为了安全起见:

((myAppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;

如果您需要设置app delegate,请将此长代码添加到id delegate = [UIApplication sharedApplication].delegate; if ([delegate respondsToSelector:@selector(managedObjectContext)]) { NSManagedObjectContext *context = id.managedObjectContext; } 的{​​{1}}:

.m

这基本上是苹果编写的代码,在大多数情况下几乎都是复制/粘贴。 只需使用appDelegate文件的名称切换@synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator; #pragma mark - Core Data stack - (void)saveContext { NSError *error = nil; NSManagedObjectContext *managedObjectContext = self.managedObjectContext; if (managedObjectContext != 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. Log(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } } // Returns the managed object context for the application. // If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. - (NSManagedObjectContext *)managedObjectContext { if (_managedObjectContext != nil) { return _managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { _managedObjectContext = [[NSManagedObjectContext alloc] init]; [_managedObjectContext setPersistentStoreCoordinator:coordinator]; } return _managedObjectContext; } // Returns the managed object model for the application. // If the model doesn't already exist, it is created from the application's model. - (NSManagedObjectModel *)managedObjectModel { if (_managedObjectModel != nil) { return _managedObjectModel; } NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"myDataModel" 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:@"myDataModel.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. */ Log(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return _persistentStoreCoordinator; } 即可。 比你myDataModel添加这些:

xcdatamodeld

现在每次需要.h时 - 从您的应用代表处获取一个。您也可以使用app delegate ...

中的方法保存@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复