Efficient way to count relationships in a database

I want to know what is the best way to get the number of interconnected relationships in many ways. Let's say I have a data model that looks like this (simplified), and I want to know the number of passengers for each tire:

data model

Currently, I can present two options:

  • Add an additional attribute for the tire object called the passenger, which will be updated every time the passenger is added / removed.

  • Each time the number of passengers should be displayed, this is done by extracting the passengers and displaying their number.

, , / . , , 2 .

: ? , NSManagedObject ( ) - , ?

+4
2

:

. , . " - ". ( )

. , ? - ...

[bus.passengers count]

... . Core Data , , , . ( , .)

. , Core Data, .

-com.apple.CoreData.SQLDebug 1

:

, -countForFetchRequest:error:.

NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"passenger"];
fetch.predicate = [NSPredicate predicateWithFormat:@"bus == %@", bus];
NSUInteger count = [context countForFetchRequest:fetch error:NULL]; // Please pass an NSError instance in real world

Safari.

+5

XCode NSManagedObject Core Data bus Passenger.

" " ( , , Core Data ). NSSet? ( Passenger), NSSet .count. , .count .

0

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


All Articles