开发者

Problem with one-to-many NSPredicate in NSFetchedResultsController

Lets say I have a core data model like this one:

Item

attributes:

name

type

relationships:

properties

Property

attributes:

name

value

realationships:

item

Each Property is connected to one Item, and each Item many properties. One Property may look exactly the same as another.

I know the value of one Property and want to get the value of another Property from the same Items. So first i fetch all Properties with that value.

    NSEntityDescription *propEntity = [NSEntityDescription entityForName:@"Property" inManagedObjectContext:self.managedObjectContext];
    NSPredicate *propPredicate = [NSPredicate predicateWithFormat:@"name like 'someName' AND value like %@", value];          
    NSFetchRequest *propRequest = [[NSFetchRequest alloc] init];
    [propRequest setEntity:propEntity];
    [propRequest setPredicate:propPredicate];  
    NSError *error = nil;

    NSArray* properties = [self.managedObjectContext executeFetchRequest:propRequest error:&error];

This works fine and I get the expected number of objects in my array. So now I want to fetch all the items that has these properties:

    NSEntityDescription *itemEntity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
    NSPredicate *itemPredicate = [NSPredicate predicateWithFormat:@"type like %@ AND ANY properties in %@", @"typeValue", properties];          
    NSFetchRequest *itemRequest = [[NSFetchRequest alloc] init];
    [itemRequest setEntity:itemEntity];
    [itemRequest setPredicate:itemPredicate];

    NSArray* items = [self.managedObjectContext executeFetchRequest:itemRequest error:&error];

This also works and I get the expected number of objects in the array. Now I have my items I want to have the values of a property with another name in these items and it's here I encounter my problem:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Property" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like 'someOtherName' AND ANY item in %@", items];

[fetchRequest setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                     开发者_高级运维                                                       managedObjectContext:self.managedObjectContext 
                                                                                            sectionNameKeyPath:@"value"
                                                                                            cacheName:@"Props"];

Now the FetchedResultsController's fetchedObjects array includes all of the properties with someOtherName not just only the ones that has an item that is in the items array.

Have I missed something here? I have tried using

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like 'someOtherName' AND item in %@", items];

and

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like 'someOtherName' AND ALL item in %@", items];

but with the same result.

Why don't I get just the properties that has item in items?

If it helps, I have the sql-query that would give the result I'm looking for:

Select distinct Property.value from Property where Property.name = 'someOtherName' and property.item in (Select Property.item from Property where Property.name = 'someName' and Property.value = 'someValueIGetFromTheUI')

Thanks in advance for any help.


I believe your SQL query

/*

SELECT DISTINCT Property.value 
FROM Property 
WHERE Property.name = 'someOtherName' 
AND Property.item IN (SELECT Property.item from Property where Property.name = 'someName' 
                      AND Property.value = 'someValueIGetFromTheUI')
*/

Translated to a Predicate for a fetch to use in your FRC

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == 'someOtherName' AND SUBQUERY(items, $item, $item.name == 'someName' AND $item.value == %@)", value];



NSEntityDescription *propEntity = [NSEntityDescription entityForName:@"Property" inManagedObjectContext:self.managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setEntity:propEntity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setPredicate:predicate]; // from above

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"value" cacheName:@"Props"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜