Baub
Baub

Reputation: 5044

Fetching a particular record from a Core Data entity

How does one choose which Core Data record to fetch?

I have one entity, "Credit Card", and I have ten actual credit cards stored in that entity. How do I say "Hey, I want to fetch THAT card?"

Upvotes: 0

Views: 1014

Answers (2)

RyanR
RyanR

Reputation: 7758

How do you know which Credit Card to fetch? You have a Person entity, who has a creditCard property which refers to a CreditCard object (different entity). You don't have to ask Core Data to perform another fetch, hitting the creditCard property on the person reference causes it to 'fault' the object and fetch the related CreditCard entity automatically. If it's a one-to-many relationship, the property is actually an NSSet representing all the CreditCards related to that person. Read the Core Data guide, it's full of great information like this.

Upvotes: 0

benzado
benzado

Reputation: 84338

You create an NSFetchRequest with a predicate that identifies the credit card you want (e.g., id = 3 or cardHolderName = "JOE SMITH"), then ask the NSManagedObjectContext to execute the fetch request, returning the result set (which will contain the one card you want).

Upvotes: 2

Related Questions