Home > iPhone SDK > Core Data - Inserting a New Managed Object
Core Data - Inserting a New Managed Object
Posted on Thursday, July 2, 2009 by android apps market for tablests
For some reason, it really bugs me that the way that you insert a new managed object into a managed object context is by using a class method on NSEntityDescription. I realize that there is no "one right" abstraction, but every time I've been away from Core Data for a while, it takes me a while to remember how to create and insert a new object because this:
is completely non-intuitive for me. Although the entity description is used in the process of inserting a new managed object, there's no way you can claim it's the primary object for that action.
I may be wrong, but I think it was this way in EOF, too. Anyway, the nice thing about having a dynamic language like Objective-C that supports categories is that you don't have to live with things that don't fit your particular way of thinking.
To me, the logical place for a method that inserts a new managed object into a managed object context, would be an instance method on the context, though I could also see an argument for it being a factory method on the managed object as well.
When I'm writing my own Core Data apps, I use this category:
NSManagedObjectContext-insert.h
NSManagedObjectContext-insert.m
This very short little category allows me to insert new objects into a context by simply doing this:
Which personally, I find a lot easier to remember than the first one.
NSManagedObject *newManagedObject = [NSEntityDescription
insertNewObjectForEntityForName:[entity name]
inManagedObjectContext:context];
is completely non-intuitive for me. Although the entity description is used in the process of inserting a new managed object, there's no way you can claim it's the primary object for that action.
I may be wrong, but I think it was this way in EOF, too. Anyway, the nice thing about having a dynamic language like Objective-C that supports categories is that you don't have to live with things that don't fit your particular way of thinking.
To me, the logical place for a method that inserts a new managed object into a managed object context, would be an instance method on the context, though I could also see an argument for it being a factory method on the managed object as well.
When I'm writing my own Core Data apps, I use this category:
NSManagedObjectContext-insert.h
#import <Cocoa/Cocoa.h>
@interface NSManagedObjectContext(insert)
-(NSManagedObject *) insertNewEntityWithName:(NSString *)name;
@end
NSManagedObjectContext-insert.m
#import "NSManagedObjectContext-insert.h"
@implementation NSManagedObjectContext(insert)
-(NSManagedObject *) insertNewEntityWithName:(NSString *)name
{
return [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:self];
}
@end
This very short little category allows me to insert new objects into a context by simply doing this:
[context insertNewEntityWityName:[entity name]];Which personally, I find a lot easier to remember than the first one.
Category Article Core Data, iPhone SDK
Powered by Blogger.
Blog Archive
-
▼
2009
(496)
-
▼
July
(61)
- We're Listening
- We're Listening
- The Iterative Web App: Links Got Shorter and Smarter
- The Iterative Web App: Links Got Shorter and Smarter
- DP aka Dataran Pahlawan Malacca.
- The Most Reliable
- Google Latitude. Now for iPhone.
- Google Latitude. Now for iPhone.
- Interesting Contrast
- Waving the Red Flag
- Microsoft's Ads Redux
- Layers of fun in Google Maps for mobile 3.2
- Layers of fun in Google Maps for mobile 3.2
- August 14 Workshop Update
- To VBO or Not to VBO...
- Improved Blender Export
- Refactoring Nav from Chapter 9
- Voyager Class Mixmaster
- Fixed & Improved Blender Export Script
- How MJ Died..
- A Little Ego Boost for a Friday
- WWDC 2009 Videos Available for Purchase
- Beginning iPhone 3 Development is Available
- Gah! Up is Down! Right is Wrong! Make it Stop!
- First Post
- Search with My Location for iPhone 3.0. And All t...
- Search with My Location for iPhone 3.0. And All t...
- Browse a collection of experts' favorite places us...
- Browse a collection of experts' favorite places us...
- New Device Rules
- Updating Project Hint
- Google Voice for Android and BlackBerry
- Google Voice for Android and BlackBerry
- Detail Editor Update: To Many Relationships
- Generic Detail Editor Update
- August 14 Workshop
- A Category on NSDate
- unusual business ideas that work Gadget
- Core Data Default Dates in Data Model
- A Kick-Ass Objective-C Programmer is a Kick-Ass C ...
- TinyPlayground Gadget
- Google Product Search for mobile now available for...
- Google Product Search for mobile now available for...
- Travelzoo UK Deals
- In Search of a Better Way: Editable Detail Views
- The Perils of Helping Others
- Core Plot
- More on Teaching
- An IPhone App Setback
- Oh, Good, Here Comes the Justice Department
- Windows Mobile: World Domination is In Sight!
- Core Data Navigation-Based Application
- iPhone Simulator Application Creator
- Wireless Carriers the Great Train Robbers of this ...
- An Exercise in Blatant Bias
- Core Data - Inserting a New Managed Object
- Google Search results optimized for feature phones...
- Google Search results optimized for feature phones...
- What, Me Teach?
- Mint Apps Promo Day is Today!
- Core Data - Determining if a Managed Object is New
-
▼
July
(61)