Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6194511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:18:32+00:00 2026-05-24T03:18:32+00:00

CoreData beginner I have a simple problem with CoreData. My model has two entities,

  • 0

CoreData beginner

I have a simple problem with CoreData. My model has two entities, now called A and B. Entity A has a to many relationship of B entities, which has a inverse relationship to entity A.

I’m retrieving entities A with this code:

NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"A"
                                          inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                           ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:descriptor]];

NSError *error = nil;
NSArray *items = [context executeFetchRequest:request error:&error];

if (error) /* ... */;

for (id item in items)
{
    /* ... */
}

[request release];
[descriptor release];

Now I’d like to retrieve, inside that loop, an array of all the objects B pointed by the relationship of A. How can I achieve this? Should I create another fetch request or there is a more practical way?

I’ve searched StackOverflow and found similar questions, but too vague sometimes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:18:32+00:00Added an answer on May 24, 2026 at 3:18 am

    NSFetchRequest has an instance method on it called -setRelationshipKeyPathsForPrefetching:.

    This method takes an array of key names that will be used to prefetch any objects defined in relationships with those key paths. Consider your example, updated with the new code:

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];
    NSString *relationshipKeyPath = @"bObjects"; // Set this to the name of the relationship on "A" that points to the "B" objects;
    NSArray *keyPaths = [NSArray arrayWithObject:relationshipKeyPath];
    [request setRelationshipKeyPathsForPrefetching:keyPaths];
    

    Now once you complete your fetch request, all of those relationship objects should be faulted in and ready to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I have a CoreData entity named, say, 'Book', which has a one-to-one relationship
I am using CoreData and have an Entity ContactList defined, which I created using
I have a CoreData model (managed object) called Item: @interface Item : NSManagedObject {
I'm wanting to make a CoreData entity called Employees, some Employees could have a
In my Core Data managed object model, I have an entity Foo with a
I have a relationship in a Core Data model that feels like it wants
In CoreData, I have the data graph with some entities, and each object is
Using CoreData (on an iPhone app) I generated my entity classes from the model
I have a coredata attribute called visitDate I want to set the date and
I have problems with my coredata sqlite DB, which hosts a book DB. After

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.