• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Two little Core Data categories
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Two little Core Data categories


  • Subject: Two little Core Data categories
  • From: Jeff LaMarche <email@hidden>
  • Date: Thu, 22 Dec 2005 15:50:45 -0500

Hey, all... just sharing two little categories I've written to make my life easier when working with Core Data. If I'm reinventing the wheel with either of these, please let me know. There's nothing earth- shattering here, but they do make for more readable, shorter code.

First: I don't like the way NSManagedObject allows you to get a dictionary of committed values and a dictionary of changed values for an entity, but doesn't offer a way to easily get a dictionary of all keys with their current value (regardless of whether they've been changed). So, I wrote this category to implement that functionality:

// Header follows -------------------
#import <Cocoa/Cocoa.h>
@interface NSManagedObject(current)
-(NSDictionary *)currentValues;
@end

// Implementation follows---------------------
#import "NSManagedObject-current.h"
@implementation NSManagedObject(current)
-(NSDictionary *)currentValues
{
NSMutableDictionary *ret = [NSMutableDictionary dictionaryWithDictionary:[self committedValuesForKeys:nil]];
[ret addEntriesFromDictionary:[self changedValues]];
return ret;
}
@end

The second category resolves one of my few significant pet peeves with Core Data - the fact that you delete objects from your context, and get objects from your context using NSManagedObjectContext, but that you insert an object into the context using NSEntityDescription (WTF?). It's a minor thing, but it irks me. I'm sure they've got a very good reason for having that method there, but it's inconsistent, hard to remember, and really difficult for new Cocoa programmers to grasp. So, I've added a method to NSManagedObjectContext to allow inserting a new entity through it:

// Header follows -------------------
#import <Cocoa/Cocoa.h>
@interface NSManagedObjectContext(insert)
-(NSManagedObject *) insertNewEntityWithName:(NSString *)name;
@end

// Implementation follows---------------------
#import "NSManagedObjectContext-insert.h"
@implementation NSManagedObjectContext(insert)
-(NSManagedObject *) insertNewEntityWithName:(NSString *)name
{
return [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:self];
}
@end

Feel free to use, modify, etc. without limitation or exception, but usual disclaimers apply (no warranty, blah blah blah).

Jeff
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Prev by Date: Re: Lots of items in an NSOutline view.
  • Next by Date: Zero kb file copied
  • Previous by thread: Re: R: R: Linking framework to PreferencesPane
  • Next by thread: Zero kb file copied
  • Index(es):
    • Date
    • Thread