Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmhaggerty/cbdd5f38b329eab2384166730924781b to your computer and use it in GitHub Desktop.
Save kenmhaggerty/cbdd5f38b329eab2384166730924781b to your computer and use it in GitHub Desktop.
@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