I have two Modals, Patient and PatientDocument like below
@objc (Patient)
class Patient: NSManagedObject {
@NSManaged var id: Int64
@NSManaged var fullName: String
@NSManaged var lastVisitDate: NSTimeInterval
@NSManaged var documents: NSSet
}
@objc (PatientDocument)
class PatientDocument: NSManagedObject {
@NSManaged var id: Int64
@NSManaged var documentType: String
@NSManaged var documentUUID: String
@NSManaged var patient: Patient
}
I have Inserted some dummy data for Patients and documents and linked them, a patient can have many documents (one-to-many relationship). On my App (IOS) i have master detail layout with 'Patients' on Master and 'PatientDocuments' on Details section. What i want is to display the related documents for a patient when one of the patients is selected.
Using prepareforSegue i can pass the selected patient to the documentsViewcontroller (one that handles the documents). While displaying documents in cellForRowAtIndexPath i'm facing trouble retrieving the related documents for that patient.
Is there a way to retrieve the documents associated with the selected Patient alone ?. I tried printing the NSSet documents that is inside the patients and it gives me the following output.
Optional(Relationship 'documents' on managed object (0x79807af0) <Patient: 0x79807af0> (entity: Patient; id: 0x79808e70 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/Patient/p2> ; data: {
careTeamMember = "0x79786680 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/Account/p2>";
documents = (
"0x7967d260 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientDocument/p12>",
"0x7968c0e0 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientDocument/p8>",
"0x7967d540 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientDocument/p9>"
);
fullName = Patient3;
id = 2;
isCPO = 1;
lastVisitDate = "2001-01-01 00:08:55 +0000";
questionnaire = (
"0x79689e00 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientQuestionnaire/p2>",
"0x7968f440 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientQuestionnaire/p5>",
"0x796a72a0 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientQuestionnaire/p3>"
);
}) with objects {(
<PatientDocument: 0x796a6300> (entity: PatientDocument; id: 0x7967d260 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientDocument/p12> ; data: <fault>),
<PatientDocument: 0x79694bf0> (entity: PatientDocument; id: 0x7968c0e0 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientDocument/p8> ; data: <fault>),
<PatientDocument: 0x7967f410> (entity: PatientDocument; id: 0x7967d540 <x-coredata://A7367643-744D-417B-BECE-734260B90D9B/PatientDocument/p9> ; data: <fault>)
)})
(Ignore the questionnaire NSSet in the output, a patient is related to another similar object containing different type of documents called questionnaire)
You need to sort the set into an array so that you can display it in an organised way. You can do that directly (using sortedArrayUsingDescriptors:
), or you can use a fetch request with a sort descriptor (preferably passed to an NSFetchedResultsController
so you don't need to load all of the documents to memory at the same time and changes are monitored).
It would be best if your documents had a timestamp or similar that you can sort by, because that makes sense to the user. You could sort by id, which may amount to the same thing.