Last active
July 6, 2016 17:18
-
-
Save kenmhaggerty/cbdd5f38b329eab2384166730924781b to your computer and use it in GitHub Desktop.
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
@interface MyObject : NSManagedObject | |
@property (nonatomic, strong, nullable) NSSet *changedKeys; | |
@end | |
... | |
@interface MyObject | |
- (void)willSave { | |
[super willSave]; | |
if (self.isUpdated && !self.isInserted) { | |
self.changedKeys = [NSMutableSet setWithArray:self.changedValues.allKeys]; | |
} | |
} | |
- (void)didSave { | |
if (!self.changedKeys || self.isDeleted) { | |
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; | |
if (self.changedKeys) { | |
userInfo[@"newValue"] = self.changedKeys; | |
} | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"kMyObjectDidSaveNotification" object:self userInfo:userInfo]; | |
} | |
if (self.changedKeys && !self.inserted) { | |
NSMutableDictionary *userInfo; | |
if ([self.changedKeys containsObject:NSStringFromSelector(@selector(name))]) { | |
userInfo = [NSMutableDictionary dictionary]; | |
if (self.name) { | |
userInfo[@"newValue"] = self.name; | |
} | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"kMyObjectNameDidSaveNotification" object:self userInfo:userInfo]; | |
} | |
if ([self.changedKeys containsObject:NSStringFromSelector(@selector(createdAt))]) { | |
userInfo = [NSMutableDictionary dictionary]; | |
userInfo[@"newValue"] = self.createdAt; | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"kMyObjectCreatedAtDidSaveNotification" object:self userInfo:userInfo]; | |
} | |
... | |
} | |
self.changedKeys = nil; | |
[super didSave]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment