Re: Core Data NSSQLiteStoreType problem
Re: Core Data NSSQLiteStoreType problem
- Subject: Re: Core Data NSSQLiteStoreType problem
- From: Chris Hanson <email@hidden>
- Date: Sat, 10 Nov 2007 23:47:01 -0800
On Nov 10, 2007, at 11:18 PM, email@hidden wrote:
I am developing a (non-document based) Core Data app and am facing
a problem with saving data in NSSQLiteStoreType.
The app has a subclass of NSManagedObject and can add to-many
relationship
through a custom accessor.
But once I save the data and close the app then restart it again,
the objects themselves remain there but the relations of some objects
disappear.
Describe the relationship in more detail. Specifically, what does its
inverse look like? If it's missing an inverse, or has a to-one
inverse instead of a to-many inverse, that could easily explain what
you're seeing; the implementation of your custom -addRelationship:
method isn't the problem.
That's not to say your -addRelationship: method is entirely correct.
If you're going to implement accessor methods for to-many
relationships, you need to do so in a fashion that's compliant with
Key-Value Coding. Otherwise you run the risk of accidentally
implementing something that matches up with what KVC expects, as -
addRelationship: does, and your results may also be unpredictable.
For Core Data, that means the accessor methods for to-many
relationships need to follow this naming pattern -- which is the
standard naming pattern for set KVC:
For adding & removing one object from the relationship:
- (void)add<Key>Object:(NSManagedObject *)object;
- (void)remove<Key>Object:(NSManagedObject *)object;
For adding & removing (union & minus) sets of objects to & from the
relationship:
- (void)add<Key>:(NSSet *)objects;
- (void)remove<Key>:(NSSet *)objects;
For intersecting sets of objects (optional) with what's in the
relationship:
- (void)intersect<Key>:(NSSet *)objects;
For getting & setting the whole relationship at once (optional):
- (NSSet *)<key>;
- (void)set<Key>:(NSSet *)objects;
This should be documented in the Key-Value Coding Programming Guide;
it's also described in detailed comments in the <Foundation/
NSKeyValueCoding.h> header file, as part of the comments for the -
[NSObject(NSKeyValueCoding) mutableSetValueForKey:] method. And it
goes without saying that your relationship names for to-many
relationships should generally be plural. :)
What this gets you is that code outside your class can just say
(assuming your relationship's name is "items")
NSMutableSet *myItems = [myObject mutableSetValueForKey:@"items"];
and then perform manipulations on myItems (e.g. -addObject:, -
unionSet:, etc.) and it will modify the relationship through your
methods automatically.
-- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden