Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your managed object context is probably not set and the entity "Event" is obviously not valid for a nil context. </p> <p>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.</p> <p><strong>UPDATE</strong></p> <p>There is a good discussion about where to keep the Core Data stack in <a href="https://stackoverflow.com/questions/1267520/where-to-place-the-core-data-stack-in-a-cocoa-cocoa-touch-application">Where to place the “Core Data Stack” in a Cocoa/Cocoa Touch application</a>. </p> <p>Here is some example code for keeping the Core Data stack in the app delegate:</p> <p>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.</p> <p><strong>YourAppDelegate.h</strong>:</p> <pre><code>@interface YourAppDelegate : NSObject &lt;UIApplicationDelegate&gt; { // Core Data stuff NSManagedObjectModel *managedObjectModel; NSManagedObjectContext *managedObjectContext; NSPersistentStoreCoordinator *persistentStoreCoordinator; // other app ivars } </code></pre> <p><strong>YourAppDelegate.m</strong>:</p> <pre><code>- (NSManagedObjectContext *) managedObjectContext { if (managedObjectContext != nil) { return managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { managedObjectContext = [[NSManagedObjectContext alloc] init]; [managedObjectContext setPersistentStoreCoordinator: coordinator]; } return managedObjectContext; } </code></pre> <p>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;</p> <p><strong>RootViewController.h</strong>:</p> <pre><code>@interface RootViewController : UITableViewController &lt;NSFetchedResultsControllerDelegate&gt; { NSFetchedResultsController *fetchedResultsController; YourAppDelegate *app; } </code></pre> <p><strong>RootViewController.m</strong>:</p> <pre><code>#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, &amp; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload