Re: fetch and predicate help
Re: fetch and predicate help
- Subject: Re: fetch and predicate help
- From: Gonzalo Castro <email@hidden>
- Date: Thu, 6 Sep 2007 22:27:30 -0400
On Sep 6, 2007, at 1:02 AM, mmalc crawford wrote:
On Sep 5, 2007, at 9:57 PM, Gonzalo Castro wrote:
I have these entities with inverse relationship: Department <-->>
Employee. I need to know if a specific department instance has
zero employees.
I can simply ask the department instance for its "employee"
property and check its count. This works fine but should I be
using a fetch to get this information instead?
No (<http://developer.apple.com/documentation/Cocoa/Conceptual/
CoreData/Articles/cdFAQ.html#//apple_ref/doc/uid/TP40001802-242985>).
Ok, this makes sense. I'd never noticed that FAQ before, thanks.
Continuing with the Department <-->> Employee example what if I have
an Employee<-->>Patent inverse relationship where Patent is another
entity. (Employees can not share patents but can each have multiple
patents in this contrived example.) How would I find employees in a
specific department who have no patents. This is similar to my
question a few days ago where I was looking for employees with no
company cars but company cars was a to-one relationship so testing
for NIL was ok. 'Patents' however is a to-many relationship. What
would the predicate look like for that query? The "(patents == NIL)"
test below is wrong.
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName: @"Employee" inManagedObjectContext: moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity: entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(patents
== NIL) AND (department == %@)", selectedDepartment];
Thanks again,
Gonzalo
If so, what's the advantage of doing a fetch vs accessing the
instance's property directly?
None -- in fact it is counter-productive. There's considerably
greater overhead in executing a fetch than simply accessing the
instance variable.
mmalc
_______________________________________________
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