Getting the minimum attribute value in an NSS set made from a one-to-many relationship in master data

I have several objects configured in Core Data, one of which is โ€œDeckโ€ and one of which is โ€œCardโ€. Cards have several numbered relationships, including id. Deck has a one-to-many relationship with cards.

What is the best way to find a card on deck that has a minimum value for some numbered attribute like id?

It is clear that I can get a list of such cards:

NSSet *cardList = self.cards;

I think I can build an expression to get the minimum:

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"id"];
NSExpression *minExpression = [NSExpression expressionForFunction:@"min:" 
            arguments:[NSArray arrayWithObject:keyPathExpression]];

But I canโ€™t understand how to use this expression to retrieve a map with a minimum id value (or just a minimum id value).

+3
1

, Key-Value:

//assuming Card has an Id property which is a number
NSNumber *minId = [deck valueForKeyPath:@"cards.@min.Id"];

NSSet *minCards = [[deck cards] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"Id==%@", minId]];

NSSet , Id deck.cards.

. .

+7

Source: https://habr.com/ru/post/1721132/


All Articles