Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSManagedObject with Rails-ish timestamps
//
// NSManagedObjectWithTimestamps.h
// Brainbow Apps, 2011
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface NSManagedObjectWithTimestamps : NSManagedObject
@property (assign) NSDate *createdAt;
@property (assign) NSDate *updatedAt;
@end
//
// NSManagedObjectWithTimestamps.m
// Brainbow Apps, 2011
//
#import "NSManagedObjectWithTimestamps.h"
@interface NSManagedObjectWithTimestamps (Private)
-(void)touchTimestamps;
@end
@implementation NSManagedObjectWithTimestamps
@dynamic updatedAt, createdAt;
- (void)awakeFromInsert {
[super awakeFromInsert];
[self touchTimestamps];
}
- (void)awakeFromFetch {
[super awakeFromFetch];
[self touchTimestamps];
}
-(void)touchTimestamps {
[self setPrimitiveValue:[NSDate date] forKey:@"updatedAt"];
if (![self primitiveValueForKey:@"createdAt"]) {
[self setPrimitiveValue:[NSDate date] forKey:@"createdAt"];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment