【问题标题】:NSManagedObjectContext nil when calling second time第二次调用时 NSManagedObjectContext nil
【发布时间】:2013-09-11 14:07:03
【问题描述】:

每次我想多次访问 Core Data 时都会出错。我有一个表格视图,我试图在其中接收数据并显示它。

我的 SecondViewController.h 中有一个变量:

#import <UIKit/UIKit.h>
#import "CooperTestModel.h"
#import "Event.h"
#import <CoreData/CoreData.h>

@interface SecondViewController : UITableViewController {
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *eventArray;
}

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSMutableArray *eventArray;

- (void) fetchRecords;

@end

和 SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController
@synthesize managedObjectContext, eventArray;

在我的 AppDelegate 中,我得到一个设置为我的 ViewController 的对象:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    SecondViewController *tableController = [[SecondViewController alloc] init];
    tableController.managedObjectContext = [self managedObjectContext];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];

    [window addSubview: [self.navigationController view]];
    [window makeKeyAndVisible];

}


- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        NSLog(@"managedObjectContext already in use. Returning instance.");
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }

    NSLog(@"managedObjectContext nil. Returning new instance.");
    return managedObjectContext;
}

然后我在tableView中获取数据:

- (void)fetchRecords {

    NSLog(@"Fetching records.");
    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];
    // Define how we will sort the records
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];
    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (!mutableFetchResults) {
        NSLog(@"Error fetching data.");
    }
    // Save our fetched data to an array
    [self setEventArray: mutableFetchResults];

    NSLog(@"Records found: %i", [eventArray count]);
}

这在第一次尝试时效果很好,但后来我收到了这个错误:

2013-09-07 19:55:03.715 CooperTest[2697:11603] managedObjectContext nil. Returning new instance.
2013-09-07 19:55:03.729 CooperTest[2697:11603] Fetching records.
2013-09-07 19:55:03.732 CooperTest[2697:11603] Records found: 3
2013-09-07 19:55:03.734 CooperTest[2697:11603] Rows in table: 3
2013-09-07 19:55:04.874 CooperTest[2697:11603] Fetching records.
2013-09-07 19:55:04.878 CooperTest[2697:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Event''
libc++abi.dylib: terminate called throwing an exception

对此有什么想法吗?我没有设置新项目并勾选“使用核心数据”选项,因为它不存在。所以我在 AppDelegate 中添加了 CoreData.framework 和一些代码。模型和类也正确(我能够在fetchRecords 的末尾存储 3 条记录)。

【问题讨论】:

  • fetchRecords 中,您是从 ivar 访问您的 managedObjectContext 吗?它来自哪里?
  • 我编辑了这个问题。你看到了
  • 您应该始终通过您创建的属性访问您的 managedObjectContext。在fetchRecords 中,您应该使用self.managedObjectContext。怀疑这是否会解决问题,但似乎发生了一些不同的事情......
  • 不,我已经尝试过了。为什么要将变量设置为零?
  • 值得创建一个新的“空应用程序”XCode 项目并查看 AppDelegate 中的核心数据堆栈代码。它与您上面的代码有点不同。您的应用程序委托似乎没有正确保留 managedObjectContext。

标签: ios objective-c core-data


【解决方案1】:

最后我只是通过使用修复它

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = appDelegate.managedObjectContext;

在 ViewControlelr 中而不是从 AppDelegate 传递它。

【讨论】:

  • 它起作用的任何原因?我见过一些示例项目,其中托管对象上下文似乎是一个单例,而无需通过应用程序委托访问它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多