Key Data - Breakup

I have a Patient object and a List object. A patient may belong to several different lists, and a list may have several different patients.

Say I have a patient who belongs to 3 lists (A, B, C). I want to remove the patient from lists A and B. Of course, I do not want to delete the lists A and B. How can I do this?

+3
source share
2 answers

While Tim's answer above is technically correct, it seems to me that this is a bit of code.

, , . :

id myPatient = ...;
id myList = ...;
[[myPatient mutableSetValueForKey:@"lists"] removeObject:myList];

, , , . , .

, -, .

[[myList mutableSetValueForKey:@"patients"] removeObject:myPatient];

:

[myPatient setLists:nil];

.

+8

, , " " "" "". , Core Data patients , lists . , , List name , NSString.

"" ( Patient ), NSManagedObject , , , . , , :

// Assuming you have some PatientManagedObject *patient:
NSSet *patientLists = [patient lists]; // Set of ListManagedObjects
for(ListManagedObject list in patientLists) {
    if([[list name] isEqualToString:@"A"] || [[list name] isEqualToString:@"B"]){
        // Now you have to build the set of patients without this patient
        NSMutableSet *listPatients = [list mutableSetValueForKey:@"patients"];
        [listPatients removeObject:patient];
    }
}

. .

+1

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


All Articles