Master data: selection from a specific property (combining relationships)

I have a basic data model as follows

alt text

The attributes Page property is a collection of DictionaryEntry , these are the values ​​for the objects of my page, like the standard NSDictionary (with the exception of all keys and values are strings)

I have a Page that has a DictionaryEntry with key="title" and value="Home" . How to generate a selection request to load this particular page?

+4
source share
2 answers

You should take a look at the predicate syntax for subqueries. You cannot use the regular keyword ANY, as this allows you to map one column to two at a time.

  NSString *keyValue = @"title"; NSString *valueValue = @"home"; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"Page" inManagedObjectContext:_context]]; [request setPredicate:[NSPredicate predicateWithFormat:@"(SUBQUERY(attributes, $a, $a.key == %@ && $a.value == %@) .@count != 0)", keyValue, valueValue]]; 

The simplest predicate ANY attributes.key = "title" AND ANY attributes.value = "home" will not work, since it also returns pages with two dicts, for example. key = 'addr' / value = 'home' and key = 'title' / value = 'pete'.

+7
source

Assuming you start knowing the "key" to DictionaryEntry "Title".

Why not create an NSFetchRequest on a DictionaryEntry object using your famous "key" when creating an NSPredicate.

  NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"DictionaryEntry" inManagedObjectContext:_context]]; [request setPredicate:[NSPredicate predicateWithFormat:@"key == %@", keyValue]]; 

Once you have a valid DictionaryEntry object, you can use your relationship to the Link page with CoreData to get your actual page.

+1
source

All Articles