Reputation: 35933
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.
Upvotes: 0
Views: 342
Reputation: 46718
Why are you doing a fetch when you have a relationship? That is heavy and expensive. Just request the products for the bundle via
[aBundle valueForKey:@"product"];
The fetch is unnecessary and forces a disk hit when you probably don't need one. Core Data most likely has the product
relationship cached.
Also, when you are assigning a product to a bundle you do not need to get a mutable set. Just set the bundle into the product via:
[product setValue:bundle forKey:@"fromBundle"];
Core Data will manage the other side of the relationship.
Upvotes: 1
Reputation: 8772
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.
Upvotes: 2