【问题标题】:NSManagedObjectContext is nil in the UITableViewController class using CoreDataNSManagedObjectContext 在使用 CoreData 的 UITableViewController 类中为零
【发布时间】:2015-03-23 10:10:40
【问题描述】:

我需要在 CoreData 的 UITableViewController 中显示一些元素,但是 NSManagedObjectContext 实例,称为 *managedObjectContext,在应用启动时为 nil。

这是我的 UITableViewController.h

@interface TableViewController : UITableViewController

@property(nonatomic,strong) NSArray *listaElementi;

@property (readonly, strong, nonatomic) NSManagedObjectContext         *managedObjectContext; //si occupa di gestire come sono strutturate le entity

@end

这是我的 UITableViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Stato" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
self.listaElementi = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
self.title = @"Stati";

最后这是我的 AppDelegate.m

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

- (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:@"Model" withExtension:@"momd"]; //SETTARE BENE IL MODELLO
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and return 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:@"Database.sqlite"];
    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] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}

你能帮帮我吗?当我创建项目时,我没有检查添加核心数据框,但是我手动实现了所有,我只是创建了模型,我只是在数据库中插入了一些元素,这可能是问题的原因吗?

【问题讨论】:

    标签: objective-c uitableview core-data nsmanagedobjectcontext


    【解决方案1】:

    在 viewDidLoad 中试试这个

    - (void)viewDidLoad {
         [super viewDidLoad];
          AppDelegate *delgate = (AppDelegate *)[[UIApplication
          sharedApplication]delegate];
          self.managedObjectContext = delegate.managedObjectContext;
          NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
          NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Stato" inManagedObjectContext:self.managedObjectContext];
         [fetchRequest setEntity:entity];
         NSError *error;
         self.listaElementi = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2020-05-04
      • 2013-01-24
      • 2014-08-27
      相关资源
      最近更新 更多