Your managed object context is probably not set and the entity "Event" is obviously not valid for a nil context.
I use a reference to my app delegate in all my view controllers so they can access the one managed object context. It sounds like others often use a singleton to manage Core Data and would get the managed object context from that.
UPDATE
There is a good discussion about where to keep the Core Data stack in Where to place the “Core Data Stack” in a Cocoa/Cocoa Touch application.
Here is some example code for keeping the Core Data stack in the app delegate:
Use Apple's standard Core Data stack implementation in YourAppDelegate. managedObjectContext is implemented as an example, but managedObjectModel and persistentStoreCoordinator must be implemented as well.
YourAppDelegate.h:
@interface YourAppDelegate : NSObject <UIApplicationDelegate> {
// Core Data stuff
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
// other app ivars
}
YourAppDelegate.m:
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
In every view controller, get a reference to the app delegate and use it to get the managedObjectContext as needed. For example, when setting up the fetchedResultsController;
RootViewController.h:
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController *fetchedResultsController;
YourAppDelegate *app;
}
RootViewController.m:
#import "RootViewController.h"
#import "YourAppDelegate.h"
@implementation RootViewController
@synthesize fetchedResultsController;
- (void)viewDidLoad {
[super viewDidLoad];
app = (YourAppDelegate*)[UIApplication sharedApplication].delegate;
}
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:app.managedObjectContext];
[fetchRequest setEntity:entity];
// setup the batch size, predicate, & sort keys, etc
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:app.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
return fetchedResultsController;
}