Key Data Relations

Suppose you had a data model that looks like this:

/-----------------------\          /-----------------------\
| Patient               |          | Medication            |
|-----------------------|          |-----------------------|
| firstName             |          | startOn               |
| lastName              |          | endOn                 |
|-----------------------|          |-----------------------|
| medications           | <<-\     |                       |
|                       |     \->> | patients              |
\-----------------------/          \-----------------------/

Thus, there is a many-to-many relationship: patients have many drugs, and medicines have many patients.

Given an object Patient, how do you get related Medicationto the latter endOn? (assumption: the patient does not have more than one medicine that ends on the same date), that is:

// patientZero is a patient with related medication records
Patient *patientZero = ...;
Medication *mostRecentMed = [patientZero mostRecentlyCompletedMedication];

How to implement the method mostRecentlyCompletedMedication?

Thank!

+3
source share
1 answer

Actually this is not a question with the main data, but a question with the Cocoa collection. I would sort the associative set medications. Assuming the Patient => Medication to-many relationship is called medications:

Patient *myPatient;

NSSet *medications = [myPatient medications];
Medication *mostRecent = [medications sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"endOn" ascending:YES]]]
                           lastObject
                          ];
+3
source

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


All Articles