-
-
Save brenthargrave/1975860 to your computer and use it in GitHub Desktop.
NSManagedObject with Rails-ish timestamps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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