Add a transient property's code via a block in Core Data (iOS & OSX)
| Originator: | gcombs | ||
| Number: | rdar://11130505 | Date Originated: | 27-Mar-2012 01:54 PM |
| Status: | Open | Resolved: | |
| Product: | Core Data | Product Version: | iOS 6 |
| Classification: | Enhancement | Reproducible: | Yes |
27-Mar-2012 01:54 PM Gregory Combs:
Summary:
I'd like to see an expansion of the Core Data framework that allows us to add a transient property's underlying code via a block assigned on an NSPropertyDescription, in order to provide an easy means to use transient properties without subclassing the managed object.
I create my Core Data model, entities, attributes, and relationships at runtime using a JSON model description. However, in the event that I want to use an NSFetchedResultsController with sections based on (for example) a User object's lastName *initial* letter. Right now, since I don't subclass the objects at design-time, if I want to use sections in the FRC I have to include the actual letter value in a separate "sectionBy" property, then make sure to change it's value whenever someone changes the User's lastName.
However, whenever I'm building the NSEntityDescription, it would be far cleaner if I merely needed to create an NSAttributeDescription (call it "sectionBy") and then set it up like follows:
NSAttributeDescription *transient = [[NSAttributeDescription alloc] init];
[transient setName:@"sectionBy"];
[transient setTransient:YES];
[transient setAttributeType:NSUndefinedAttributeType];
[transient setAttributeValueClassName:@"NSString"];
// The Good Stuff
[transient setTransientValueWithBlock:(id (^)(void)){
NSString *input = [self valueForKey:@"lastName"];
if (!input || !input.length)
return nil;
return [input substringToIndex:1];
}];
https://gist.github.com/grgcombs/2219225
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!