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

  • SEARCH
  • Home
  • 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 6725781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:50:53+00:00 2026-05-26T09:50:53+00:00

I have two entities: products and bundles. Each one has its class. A product

  • 0

I have two entities: products and bundles. Each one has its class. A product can be in multiple bundles.

Entities are defined like this:

PRODUCTS
name, string
number, integer 16
fromBundle = to-many relationship to product

BUNDLE
name, string
number, integer 16
product = to-many relationship to fromBundle

Products were assigned to bundle like this:

// suppose bundle 1 is composed of products 1, 2, 3 and 4.
NSArray *myProd = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:1],
    [NSNumber numberWithInt:2],
    [NSNumber numberWithInt:3],
    [NSNumber numberWithInt:4],
    nil];

int bundleNumber = 1;
NSString *bundleName = @"My Bundle";
Bundle *aBundle = nil;

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];

request.entity = [NSEntityDescription entityForName:@"Bundle" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat: @"(number == %d)", bundleNumber];
NSError *error = nil;
aBundle = [[context executeFetchRequest:request error:&error] lastObject];

// as the bundle does not exist, this will run
if (!error && !aBundle) {
    aBundle = [NSEntityDescription insertNewObjectForEntityForName:@"Bundle" inManagedObjectContext:context];
    aBundle.string = bundleName;
    aBundle.Number = [NSNumber numberWithInt:bundleNumber];

    for (NSNumber *umNum in myProd) {

            // the product with number = aNum is retrieved... yes it is valid at this point 
            Product *oneProduct = [ProductWithNumber:umNum inManagedObjectContext:context];
            NSMutableSet *mutableSet = [oneProduct mutableSetValueForKey:@"fromBundle"];
            [mutableSet addObject:aBundle];
    }
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

// everything is fine at this point.

Now I wish to retrieve a list of all products that belong to a specific bundle…

To do that, I am using this method on Bundle class

+ (NSArray *)ProductsInBundle:(Bundle*)aBundle inManagedObjectContext:(NSManagedObjectContext *)context
{
    NSArray *all = nil;


    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    request.entity = [NSEntityDescription entityForName:@"Products" inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"(fromBundle == %@)", aBundle];

    NSError *error = nil;
    all = [context executeFetchRequest:request error:&error]; // crashes here

    return all;
}

it crashes on the assigned line on the last method with the message “to-many key not allowed here” when I try to do this

NSArray *allProductsInBundle = [Bundle ProductsInBundle:aBundle inManagedObjectContext:self.managedObjectContext];

aBundle is valid at this point.

  • 1 1 Answer
  • 1 View
  • 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-26T09:50:53+00:00Added an answer on May 26, 2026 at 9:50 am

    I think your predicate is wrong. You don’t have a bundle property, but a fromBundle property.

    If it is really fromBundle, then your predicate should be:

    equest.predicate = [NSPredicate predicateWithFormat:@"(fromBundle == %@)", aBundle];
    

    EDIT:

    If you are trying to do operations on to-many relationships then you’ll need to use the aggregate functions for the predicate. I think for your case you’ll want the IN operation.

    http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-215891

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

Sidebar

Related Questions

I have two Doctrine entities that have a one-to-many relationship, like this: License class
I have two entities like this: public class Service { public virtual int ServiceId
I have two entities in 1:n relationship: Category and Product. public class Category {
I have two entities: public class Product { [HiddenInput(DisplayValue=false)] public int ProductID { get;
I have a GWT applications that contains Products and Experts, and one Product can
I have two entities, a Shelf and a Product : public class Shelf {
I have two entities with a Unidirectional Many-to-One mapping. Here's Product : use Doctrine\Common\Collections\ArrayCollection;
I have two entities that are something like that: public class Movie { public
I have two entities: Recipe and Ingredient. Entites: public class Ingredient { public int
I have two entities A and B. public class A{ @Id @GeneratedValue private Integer

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.