在子类中实现时,不会调用NSManagedObject
方法,即awakeFromInsert
,等。可能是什么原因?实体“类”在实体模型编辑器中设置为子类。awakeFromFetch
NSManagedObject
事件.m
#import "Event.h"
@implementation Event
@dynamic timeStamp;
- (void)awakeFromInsert {
NSLog(@"%s", __FUNCTION__);
[super awakeFromInsert];
}
- (void)awakeFromFetch {
NSLog(@"%s", __FUNCTION__);
[super awakeFromFetch];
}
@end
MyViewController(插入)
- (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();
}
}
这是模型编辑器的屏幕截图。
注意:我在 Mountain Lion 上使用 Xcode 4.4.1 (iOS SDK 5.1),ARC ON。