Adding to-many objects programmatically
-
Hi all,
I need to add a set of default objects to my core data store when the
application is launched for the first time. (I store the properties for
these objects in the app's plist.) The two objects (Types and Categories)
are both to-many relationships - one type has many categories, and one
category can appear in multiple types. I can read the plist and create each
object just fine, but I don't have any idea how to add the objects to the
other's relationship. I tried to add the objects one-by-one and it said it
wanted an NSSet. Here's my flow, but I don't know about the code...
1. Create all Categories objects
-- while creating a category, add object to a NSMutableSet
2. Create all Type objects
-- when creating object, use setValue forKey to set the relationship to the
NSMutableSet
I haven't gotten this to work yet. Does this sound like I'm on the right
path, or am I doing too much work? Is there a different way to
programmatically insert default many-to-many objects?
Thanks... -
On Jan 15, 2010, at 8:52 pm, Jenny M wrote:
> but I don't know about the code...Modifying to-many relationships is described here:
>
<http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreD
ata/Articles/cdUsingMOs.html#//apple_ref/doc/uid/TP40001803>
What did you try that didn't work?
mmalc -
I've been setting it using key-value coding: [object setValue:mySet
forKey:categories] so far, so that's good to know I'm on the right track.
Though at the moment I am apparently having difficulties comparing strings,
because I can't get my log messages to appear in the console.... hmm.
Okay, duh, I was not comparing strings properly. Okay, so my plan worked
fine.... cancel that, again, sigh. I feel silly, again.
I guess my question is also, was there any easier way to do it?? Is that how
it's normally done?
Modifying to-many relationships is described here:
>
<
> ...<<A href="http://www.google.com/url?sa=D&q=http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.html%23//apple_ref/doc/uid/TP40001803&usg=AFQjCNHb1oYI2EmfEkjyS_eShxw1CLfSPA">http://www.google.com/url?sa=D&q=http://developer.apple.com/mac/library
/documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.html%23//apple
_ref/doc/uid/TP40001803&usg=AFQjCNHb1oYI2EmfEkjyS_eShxw1CLfSPA>>
>
>
> What did you try that didn't work?
> mmalc
>
-
On Jan 15, 2010, at 10:00 pm, Jenny M wrote:
> I guess my question is also, was there any easier way to do it?? Is that howIt's not clear exactly what you're referring to.
> it's normally done?
>
Do you mean, is the typical pattern that which you described earlier, namely:
> I've been setting it using key-value coding: [object setValue:mySet
> forKey:categories]
>
No, that's not how the documentation recommends that you do it.
"Core Data automatically generates efficient public and primitive get and setaccessor methods for modeled properties (attributes and relationships) of managed object classes (see âManaged Object Accessor Methodsâ). When you access or modify properties of a managed object, you should use these methods directly."
[...]
"Typically, however, you do not want to set an entire relationship, instead you want to add or remove a single element at a time. To do this, you should use mutableSetValueForKey: or one of the automatically-generated relationship mutator methods (see âDynamically-Generated Accessor Methodsâ):"
mmalc -
When I tried to use the getter/setter, I get a build warning that says
the NSManagedObject may not respond to that method. I noticed in the
documentation, they're referring to the entities themselves - Employee
and Department. I don't use custom classes, just the data model, so
I've been referring to them as NSManagedObjects the whole time,
though... how would I refer to them by their names?
The objects do load, I ran the program regardless of warnings and it
did set the categories and references. So, how would I get it to build
without those warnings?
On Jan 15, 10:13Â pm, mmalc Crawford <mmalc_li......> wrote:
> "Core Data automatically generates efficient public and primitive get and setaccessor methods for modeled properties (attributes and relationships) of managed object classes (see âManaged Object Accessor Methodsâ). When you access or modify properties of a managed object, you should use these methods directly."
>
-
On Jan 15, 2010, at 10:57 pm, Jenny M wrote:
> The objects do load, I ran the program regardless of warnings and itAll of this is covered in the documentation:
> did set the categories and references. So, how would I get it to build
> without those warnings?
>
<http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreD
ata/Articles/cdAccessorMethods.html#//apple_ref/doc/uid/TP40002154-SW1>
mmalc -
Cast to the actual type before calling the method.
Instead of [ managedObject method1 ]
do
[ (Employee*)managedObject method1 ]
On 16-Jan-2010, at 14:57, Jenny M <safflower...> wrote:
> When I tried to use the getter/setter, I get a build warning that says
> the NSManagedObject may not respond to that method. I noticed in the
> documentation, they're referring to the entities themselves - Employee
> and Department. I don't use custom classes, just the data model, so
> I've been referring to them as NSManagedObjects the whole time,
> though... how would I refer to them by their names?
>
> The objects do load, I ran the program regardless of warnings and it
> did set the categories and references. So, how would I get it to build
> without those warnings?
>
>
> On Jan 15, 10:13 pm, mmalc Crawford <mmalc_li......> wrote:
>> "Core Data automatically generates efficient public and primitive
>> get and setaccessor methods for modeled properties (attributes and
>> relationships) of managed object classes (see âManaged Object Acce
>> ssor Methodsâ). When you access or modify properties of a managed
>> object, you should use these methods directly."
>>
-
On Jan 16, 2010, at 12:17 am, Roland King wrote:
> Cast to the actual type before calling the method.No; the OP stated, "I don't use custom classes", so this won't work.
> Instead of [ managedObject method1 ]
> do
> [ (Employee*)managedObject method1 ]
>
Follow the pattern described in the documentation:
<http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreD
ata/Articles/cdAccessorMethods.html#//apple_ref/doc/uid/TP40002154-SW1>
mmalc


