views:

226

answers:

1

Hey all,

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? :))

Anyway, I have a standard Objective-C project, with a DataModel file. It contains an Entity, BatteryEvent, with properties, charge and event.

I then have PowerListener.m (and .h). PowerListener.m is implemented as follows:

    @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

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:

2009-08-19 17:59:46.078 BatteryApp[5851:813] +entityForName: could not locate an NSManagedObjectModel for entity name 'BatteryEvent'

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?

I've tried passing another kind of NSManagedObjectContext to the callback function as well. I followed this guide: Core Data Guide, but, again same error...

I'm at my wits end!

Any help appreciated!

A: 

It looks like your app isn't loading the managed object model as a part of the launch and/or Core Data stack initialization.

Where is your model loaded?

Also, make sure you spelled the entity name correctly in the model.

bbum
It's called BatteryApp_DataModel.xcdatamodelI have it in a dir called Models.Do I have to do anything specifically to load the model? I've got what you see above, plus what was on the link I updated the post with. (http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreDataUtilityTutorial/Articles/05_createStack.html)It's for the iPhone, but I guessed the Core Data stack was probably the same...
Fafnr