Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brenthargrave/1975860 to your computer and use it in GitHub Desktop.
Save brenthargrave/1975860 to your computer and use it in GitHub Desktop.
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