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 4085960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:39:27+00:00 2026-05-20T18:39:27+00:00

I have the following Core Data setup: Project has-many Answer Field has-many Answer Answer

  • 0

I have the following Core Data setup:

Project has-many Answer
Field has-many Answer
Answer has-one Field
Answer has-one Project

I need to find the Answer for each Field that is also owned by Project. I’m currently using a predicate for this and executing a fetch request:

NSEntityDescription *answerEntity = [NSEntityDescription entityForName:@"Answer" inManagedObjectContext:self.managedObjectContext];
NSPredicate *answerPredicate = [NSPredicate predicateWithFormat:@"ANY project == %@ && field == %@", self.project, self.field];          
NSFetchRequest *answerRequest = [[NSFetchRequest alloc] init];
[answerRequest setEntity:answerEntity];
[answerRequest setPredicate:answerPredicate];

NSError *error = nil;
NSArray *predicates = [self.managedObjectContext executeFetchRequest:answerRequest error:&error];

I’m still new to Core Data but I believe the fetchRequest is querying the database each time I call it, is there a more efficient way of finding these Answer objects?

  • 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-20T18:39:28+00:00Added an answer on May 20, 2026 at 6:39 pm

    If I understand correctly, you already have a Field object and an Project object and you want to find the Answer objects they have in common.

    If so, the solution is a simple intersect set operation:

    NSSet *answersInCommon=[[aFieldObj mutableSetValueForKey:@"answers"] intersectSet:[aProjectObj mutableSetValueForKey:@"answers"]];
    

    … which will return only those Answer objects that appear in both relationships.

    Update:

    @pdenya in comment provides an enhancement :

    Just want to clarify a minor error and a small point that makes this less than ideal. intersectSet returns (void) so the syntax for this would be:

    NSMutableSet *answers=[field mutableSetValueForKey:@"answers"]; 
    [answers intersectSet:[project mutableSetValueForKey:@"answers"]]; 
    

    This solution also modifies the aFieldObj.answers array meaning you can’t use this while iterating. setWithSet clears this right up. Example:

    NSMutableSet *answers = [NSMutableSet setWithSet:[project mutableSetValueForKey:@"answers"]]; 
    [answers intersectSet:[field mutableSetValueForKey:@"answers"]];
    

    @pdenya’s is the correct form.

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

Sidebar

Related Questions

I have setup my first core data project. Everything works fine until I do
Given the following Core Data Model: -> : to-one releationship ->>: to-many relationship Entity
Say I have core data objects of type obj that has a property propertyA
So I have the following code that sorts a core data fetch by the
I have a Core Data store which contains a number of MediaItem entities that
I have the following relationship defined in Core Data Person --> Worked <-- Job
I am following Apples Core Data Tutoriol but implementing it into my own Project,
I'm modeling my app in Core Data. I have an object called 'stats' that
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);
I have following situation. A main table and many other tables linked together with

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.