Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Shaun Merritt
Shaun Merritt
2,126 Points

Connecting Two Users Together With Parse

Hello! I have a question that I have been trying to solve for the past few days, but just haven't been able to do it. My application differs in that there are 2 types of users: A Doctor, and a Patient. They are both stored in the Users class on Parse, the only difference is the object "isDoctor" changes based on how they sign up. I have this working. What I now need to do is add the ability for a doctor to register a new patient. I found something on github that does this through cloud code, but I have no idea how to implement into my program. I will get the info for the new patient from textfields. Here is the link to the github project: https://gist.github.com/mikevansnell/5140654. I have this loaded as my cloud code, my only problem is calling it with all the right parameters from my application.Thanks so much! Any help is greatly appreciated! Have a great day!

1 Answer

Stone Preston
Stone Preston
42,016 Points

you could use a PFRelation. you wouldnt need to use cloud code, though im sure you could.

The simplest way to do this would be instead of using the a form with textfields use a tableView that shows all the available patients. Then the doctor user would select a patient, select done, and then that patient user would be added to a PFRelation that belongs to the doctor user like so:

//inside the tableView delegate method didSelectRowAtIndexPath:
//we need to save the patient the doctor selected from the tableView
///self.allPatients is an array that stores all the patients obtained from a PFQuery
self.selectedPatient = [self.allPatients objectAtIndex:indexPath.row];

then in the IBAction:

//inside IBAction for the done button
PFUser *currentDoctor = [PFUser currentUser];
PFRelation *hasPatientRelation = [currentDoctor relationForKey:@"patients"];
//add the patient selected in the tableView
[hasPatientRelation addObject:self.selectedPatient];
[currentDoctor saveInBackground]

If you used a text field based form, you would have to query the backend for the user using the info from the form. You would need to handle the case where the user did not exist in teh backend (the query found 0 results)Once you found that user, the process would be the same as the process of creating the relation above, except you would add the user returned by the query.

I like the tableView method better because you wouldnt have to double check that the user actually existed first like you would with the form. The tableView just shows all the available patients.

Shaun Merritt
Shaun Merritt
2,126 Points

Hi, thats great for once I have the user created. Currently what is happening however, is when the doctor registers the patient, It signs the patient in on the device. Is there any way to have it so when I create a user it stores to parse, but does't log them in? Thanks so much.

Stone Preston
Stone Preston
42,016 Points

can you explain a little more what exactly you want to do. you want the doctor to register the patient, do you want the patient to actually be a user of the app or is this just for a record?

if you just want a record of the patient, just use a new Patient class that stores the patient data and use that instead of PFUser.

Shaun Merritt
Shaun Merritt
2,126 Points

Essentially the doctor will create the new patient. The patient needs to be a user. The patient will then be able to log in with the credentials that the doctor set up, and then I will establish a PFRelation between the patient and doctor using the code you provided.

Stone Preston
Stone Preston
42,016 Points

ah ok that makes sense. you probably do need to use cloud code then. Ive never used it so I cant really help you with that

Stone Preston
Stone Preston
42,016 Points

ah ok that makes sense. you probably do need to use cloud code then. Ive never used it so I cant really help you with that

Shaun Merritt
Shaun Merritt
2,126 Points

ok. thanks for your help with relating them, now I know how to relate them once I create them.