Question

I’m relatively new at iOS programming and Core Data, so please forgive me if this is a stupid question. In my project I have two entities. Entity (1) is named “Doctor”, entity (2) is “Patient”. The relationship between Doctor and Patient is named “patients”. The inverse is named “doctor”. I have a one to many relationship made between doctor and patient. The entities have been sub-classed. I have the root view controller displaying doctor names. I want to be able to select a cell of a doctor and display his patients on the segued view controller. His patients are contained in an NSSet. I’m stumped here. Do I pass that NSSet in the segue code, or do I use a predicate in the fetch request to fetch only the applicable patient names? Any code example you could provide would be greatly appreciated.

Was it helpful?

Solution

Just pass the Doctor object obtained when the user tapped the cell to your detail controller. Then in the detail controller use doctor.patients (the NSSet). This is the simplest option and will work well if you aren't making any changes to the data. You're probably displaying the patients in a table view so you will want to use NSSortDescriptor to organise the patients before displaying them (then they will be in an array and you use objectAtIndex:).

If you're going to change the data it'll still work, but you need to know about the change in some way so you can update the UI.

If you already have a fetched results controller, that's fine and will monitor changes for you. In that case use the doctor in the predicate format:

[NSPredicate predicateWithFormat:@"doctor = %@", self.doctor];

Either way, pass the doctor object to the new view controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top