Vikings
Vikings

Reputation: 2527

Core Data Predicate To Many

I have a core data model that has a one to many relationship, there is a category, and it can contain many subcategories.

Category <---->> Subcategory

I am trying to perform a fetch that checks if a particular Category contains a Subcategory with a particular name. Let's say I have two categories below, I want to fetch to see if there are any subcategories name "Apple" in the Category named "Fruits".

 Vetegables
    - Carrot
    - Lettuce

 Fruits
    - Apple
    - Orange
    - Pear

Code:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory
                                   inCategory:(Category *)category
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SubCategory" inManagedObjectContext:self.beer.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@", subCategory];
    [fetchRequest setPredicate:predicate];

    NSError *error;
    NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    if (fetchedObjects != nil && fetchedObjects.count > 0) {
        return [fetchedObjects objectAtIndex:0];
    }
    else {
        return nil;
    }
}

Upvotes: 0

Views: 399

Answers (1)

Tomasz Zabłocki
Tomasz Zabłocki

Reputation: 1326

Try looking for category entity which has subcategory you like not the other way around, then return category.subcategory from results. I am not sure how your NSManagedObjects look like but try this:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory
                                   inCategory:(Category *)category
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.beer.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subcategory.name == [c] %@ AND name == %@", subCategory, category.name];
    [fetchRequest setPredicate:predicate];

    NSError *error;
    NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    if (fetchedObjects != nil && fetchedObjects.count > 0) {
        Category *categoryManagedObject = [fetchedObjects objectAtIndex:0];
        return categoryManagedObject.subcategory;
    }
    else {
        return nil;
    }
}

...or just add category to your predicate, you should have it in your subcategory managed object definition:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory
                                       inCategory:(Category *)category
    {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.beer.managedObjectContext];
        [fetchRequest setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@ AND category == %@", subCategory, category];
        [fetchRequest setPredicate:predicate];

        NSError *error;
        NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error];

        if (fetchedObjects != nil && fetchedObjects.count > 0) {
            return [fetchedObjects objectAtIndex:0];
        }
        else {
            return nil;
        }
    }

Upvotes: 2

Related Questions