Mustafa
Mustafa

Reputation: 20564

NSManagedObject methods are not getting called

The NSManagedObject methods i.e. awakeFromInsert, awakeFromFetch, etc are not getting called, when implemented in the NSManagedObject subclass. What could be the reason? The Entity "Class" is set to subclass in the Entity model editor.

Event.m

#import "Event.h"

@implementation Event

@dynamic timeStamp;

- (void)awakeFromInsert {
    NSLog(@"%s", __FUNCTION__);
    [super awakeFromInsert];
}

- (void)awakeFromFetch {
    NSLog(@"%s", __FUNCTION__);
    [super awakeFromFetch];
}

@end

MyViewController (Insert)

- (void)insertNewObject:(id)sender {
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];

    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}

And here's a screen-shot of the model editor. enter image description here

Note: I'm using Xcode 4.4.1 (iOS SDK 5.1) on Mountain Lion, with ARC ON.

Upvotes: 0

Views: 613

Answers (1)

wkhatch
wkhatch

Reputation: 2741

make sure your classes are added to the targets, and also make sure they're specified in the model. If you don't specify the subclass within the model, per entity, it won't matter that you have the classes included in the target; you'll just be dealing with an NSManagedObject.

Upvotes: 4

Related Questions