title: Using NSManagedObject manually - something wrong with the NSManagedContext I get?
body: <p>I'm new to Cocoa programming, and decided for my first project to create a small application to monitor and remember certain battery stats for my laptop. (I have it plugged in most of the time, and apple recommend you discharge it now and again, so why not try to make a small program to help you remember to do this? :))</p>
  
  <p>Anyway, I have a standard Objective-C project, with a DataModel file.
  It contains an Entity, BatteryEvent, with properties, charge and event.</p>
  
  <p>I then have PowerListener.m (and .h).
  PowerListener.m is implemented as follows:</p>
  
  <pre><code>    @implementation PowerListener
  void myPowerChanged(void * context) {
      printf("Is charging: %d\n", [PowerFunctions isCharging]);
      printf("Is on ac: %d\n", [PowerFunctions isOnAC]);
      printf("Charge left: %d\n", [PowerFunctions currentCapacity]);
      printf("Powerchanged\n");
      NSManagedObject *newBatteryEvent = [NSEntityDescription 
                          insertNewObjectForEntityForName:@"BatteryEvent" 
                                       inManagedObjectContext:context];
  }
  
  - (PowerListener*) init {
      self = [super init];
      if(self) {
          CFRunLoopSourceRef loop = IOPSNotificationCreateRunLoopSource(myPowerChanged, [[NSApp delegate] managedObjectContext]);
          CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, kCFRunLoopDefaultMode);
          CFRelease(loop);
      } else {
          printf("Error\n");
      }
      return self;
  }
  @end
  </code></pre>
  
  <p>My problem is that once I run this (inited through main.m's main-method) and the power actually DOES change, I get thrown an error where I try to create the new BatteryEvent object:</p>
  
  <pre><code>2009-08-19 17:59:46.078 BatteryApp[5851:813] +entityForName: could not locate an NSManagedObjectModel for entity name 'BatteryEvent'
  </code></pre>
  
  <p>So it looks to me like I have the wrong ManagedContext? How do I get the right one? 
  Am I even on the right track here?</p>
  
  <p>I've tried passing another kind of NSManagedObjectContext to the callback function as well.
  I followed this guide: <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreDataUtilityTutorial/Articles/05_createStack.html" rel="nofollow noreferrer">Core Data Guide</a>, but, again same error...</p>
  
  <p>I'm at my wits end!</p>
  
  <p>Any help appreciated!</p>