• 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
Re: CoreData relationship/KVC question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CoreData relationship/KVC question


  • Subject: Re: CoreData relationship/KVC question
  • From: Frédéric Testuz <email@hidden>
  • Date: Fri, 30 Jan 2009 22:41:34 +0100

Le 30 janv. 09 à 20:14, Jean-Nicolas Jolivet a écrit :

I've been working on my first CoreData project and I have a question regarding dynamically generated accessors for to-many relationships and KVC...

I'll keep it as simple as possible, let's assume I have a Department entity which contains many Employees entities....
from what I understand in the doc, CoreData should automatically generate those methods for Department:


-addEmployees:(NSSet *)values
-addEmployeesObject:(NSManagedObject *)value

My question is, how would I access these 2 methods using KVC accessors?

For example, if I want to set the employee's name attribute I can use: [anEmployee setValue:@"John Doe" forKey:@"name"];

Following this pattern, could I use
[aDeparment setValue:anEmployee forKey@"employees]; // Assuming anEmployee is an NSManagedObject*


or

[aDeparment setValue:someEmployees forKey@"employees]; // Assuming someEmployees is an NSSet*


In other words, are relationship also KVC and, will CoreData aumatically call the correct method depending on the type of the "value" I'm setting (either an NSSet or an NSManagedObject) ?

No. I never tried, but I'm pretty sure you will obtain garbage, at least for you first proposition. The second will simply replace the old employees set with someEmployees.


I know I can declare an NSManagedObject category with a bunch of accessors etc.. to suppress compiler warning but I was wondering about the KVC get/set as I am using them too....

If you want to use the KVC method use -mutableSetValueForKey or - mutableSetValueForKeyPath: . In return you have a proxy object for the key (keypath). Send to this proxy usual mutable set methods, the KVO notifications will be send for you. EX :


[[aDepartment mutableSetForKey:@"employees"] addObject:someEmployees];
[[aDepartment mutableSetForKey:@"employees"] unionSet:someEmployees];

Frédéric


_______________________________________________

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


References: 
 >CoreData relationship/KVC question (From: Jean-Nicolas Jolivet <email@hidden>)

  • Prev by Date: Re: WritePipeAsync returns error code e00002c2 (Bad Argument)
  • Next by Date: Re: CoreData relationship/KVC question
  • Previous by thread: CoreData relationship/KVC question
  • Next by thread: Re: CoreData relationship/KVC question
  • Index(es):
    • Date
    • Thread