PDA

View Full Version : [Resolved] Struggling with Core Data: Part 2




RonC
May 23, 2011, 08:00 PM
Next in what promises to be an unending series of questions about Core Data. This one has to do with following relationships and queries.

Here's a simple data model:286608

There are two managed objects, Owner and Thing. Owner owns many things, but a thing is only owned by one owner. I want to test to see if a given thing exists, and if it does exist, if it's owned by a given owner (theOwner). If it doesn't exist, I'm going to create it and link it to theOwner (newThing.owner = theOwner).

I know how to test if a given thing exists (with the assumption that the thing's name property is unique), but I don't know how to craft a fetch request such that it only looks for the ones that have owner = theOwner. Any pointers to how to create that fetch request?

Is it as simple as looking in the theThing.things set to see if the Thing I'm looking for is there? If I only fetch theOwner, does it populate the set appropriately? Is this where the magic of "faulting" comes into play?

Thanks!

Ron



Cromulent
May 23, 2011, 08:45 PM
Next in what promises to be an unending series of questions about Core Data. This one has to do with following relationships and queries.

Here's a simple data model:286608

There are two managed objects, Owner and Thing. Owner owns many things, but a thing is only owned by one owner. I want to test to see if a given thing exists, and if it does exist, if it's owned by a given owner (theOwner). If it doesn't exist, I'm going to create it and link it to theOwner (newThing.owner = theOwner).

I know how to test if a given thing exists (with the assumption that the thing's name property is unique), but I don't know how to craft a fetch request such that it only looks for the ones that have owner = theOwner. Any pointers to how to create that fetch request?

Is it as simple as looking in the theThing.things set to see if the Thing I'm looking for is there? If I only fetch theOwner, does it populate the set appropriately? Is this where the magic of "faulting" comes into play?

Thanks!

Ron

You want to look into the NSPredicate class for such things. This is really the class you should use for any queries you want to execute on your Core Data models as it allows you quite a bit of flexibility (think of it as Core Data's version of SQL).

Look here for some info:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html

RonC
May 27, 2011, 01:02 PM
You want to look into the NSPredicate class for such things. This is really the class you should use for any queries you want to execute on your Core Data models as it allows you quite a bit of flexibility (think of it as Core Data's version of SQL).

Look here for some info:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html

Thanks. I did just that and have it working quite nicely now. I think I'm getting the hang of Core Data, Predicates, and NSFetchedResultsController.

The predicate I am using, made appropriate for this example, is (name = "TheThing") AND (owner.name = "TheOwner")
(the quoted values are filled in via predicateWithFormat: %@ fields).

Ron