Updating Parse Class By Adding Favorite's Pointer To User

248 views Asked by At

I'm trying to add a new feature to my app, the app features Jobs from around the world. Currently the user can tap on a tab bar button and see a list of jobs to choose from. From that table view controller the user can choose to take a look at a job by tapping the table view cell which brings them to a detailed view that displays multiple text fields and text views explaining the job.

I've added a new view called Favorites, so that if the user taps a star on the detailed view, it would add the PFUser to a pointer column in parse called Favorites to that specific job.

However, this isn't working as planned. Instead it's creating a new Job within my parse class that leaves all the fields blank except for the pointer column. My question is how would I be able to associate a specific user to the job they are viewing, so that I can pull it up later in the Favorites tab of my app?

My parse class data consists of the following:

"Apply"  (array)
"Clearance"  (string)
"Email" (array)
"Job_Description"  (array)
"Location"  (string)
"POC" (array)
"Phone" (array)
"Position" (string)
"Qualifications"  (array)
"Rotation"  (string)
"Type" (string)
"imageFile" (file)
"createdBy" (pointer<user>)
"Favorites"  (pointer<user>)

#import "JobDetailViewController.h"
#import <Parse/Parse.h>

@interface JobDetailViewController ()

@end

@implementation JobDetailViewController

@synthesize jobPhoto;
@synthesize RotationLabel;
@synthesize QualificationsTextView;
@synthesize Job_DescriptionTextView;
@synthesize TypeLabel;
@synthesize LocationLabel;
@synthesize ClearanceLabel;
@synthesize PositionLabel;
@synthesize job;
@synthesize POC;
@synthesize Email;
@synthesize Phone;
@synthesize Apply;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_Scroller setScrollEnabled:YES];
    [_Scroller setContentSize:CGSizeMake(320, 2200)];

    [self dismissKeyboard];
    self.PositionLabel.text = job.position;
    self.RotationLabel.text = job.rotation;
    self.LocationLabel.text = job.location;
    self.TypeLabel.text = job.type;
    self.ClearanceLabel.text = job.clearance;
    jobPhoto.file = (PFFile *)job.imageFile;
    [jobPhoto loadInBackground];

    NSMutableString *pocText = [NSMutableString string];
    for (NSString* poc in job.poc) {
        [pocText appendFormat:@"%@\n", poc];
    }
    self.POC.text = pocText;

    NSMutableString *emailText = [NSMutableString string];
    for (NSString* email in job.email) {
        [emailText appendFormat:@"%@\n", email];
    }
    self.Email.text = emailText;


    NSMutableString *phoneText = [NSMutableString string];
    for (NSString* phone in job.phone) {
        [phoneText appendFormat:@"%@\n", phone];
    }
    self.Phone.text = phoneText;

    NSMutableString *applyText = [NSMutableString string];
    for (NSString* apply in job.apply) {
        [applyText appendFormat:@"%@\n", apply];
    }
    self.Apply.text = applyText;

    NSMutableString *qualificationsText = [NSMutableString string];
    for (NSString* qualifications in job.qualifications) {
        [qualificationsText appendFormat:@"%@\n", qualifications];
    }
        self.QualificationsTextView.text = qualificationsText;

    NSMutableString *job_descriptionText = [NSMutableString string];
    for (NSString* job_description in job.job_description) {
        [job_descriptionText appendFormat:@"%@\n", job_description];
    }
    self.Job_DescriptionTextView.text = job_descriptionText;
}

- (void)viewDidUnload
{
    [self setJobPhoto:nil];
    [self setPositionLabel:nil];
    [self setRotationLabel:nil];
    [self setLocationLabel:nil];
    [self setTypeLabel:nil];
    [self setQualificationsTextView:nil];
    [self setJob_DescriptionTextView:nil];
    [self setPOC: nil];
    [self setPhone:nil];
    [self setEmail:nil];
    [self setApply:nil];
    [self dismissKeyboard];

    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

-(void) dismissKeyboard {
    [Email resignFirstResponder];
    [POC resignFirstResponder];
    [Phone resignFirstResponder];
    [Job_DescriptionTextView resignFirstResponder];
    [QualificationsTextView resignFirstResponder];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
 return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) favoriteSuccess {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Added job to Favorites!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}


- (void) favoriteFailed {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ooooops!" message:@"Error occurred while adding to Favorites!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}


- (IBAction)favoriteButtonAction:(id)sender {

    PFObject *objectLike = [PFObject objectWithClassName:@"Jobs"];
    [objectLike setObject:[PFUser currentUser] forKey:@"Favorites"];
    [objectLike saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            [self favoriteSuccess];
        }
        else {
            [self favoriteFailed];
        }
    }];

}
@end

- (IBAction)favoriteButtonAction:(id)sender {
    PFObject *user = [PFObject objectWithClassName:@"User"];
    PFObject *jobs = [PFObject objectWithClassName:@"Jobs"];
    PFRelation *relation = [user relationForKey:@"Favorites"];
    [relation addObject:jobs];
    [user saveInBackground];
}
1

There are 1 answers

11
Wain On

Rather than using a pointer or array of pointers you should likely be using a relationship from User to Job. Then, when the button is tapped you add (or remove) the favourite to the relation and save the user.

Where you call objectWithClassName You're creating a new Job instance, which isn't what you want. You should be referencing the instance you downloaded to populate the UI. But, if you did that, any job could only be favourited by one user. Also, the job should really be read-only to everyone except the person who created it.

So, change to using a relationship.


Add a job to the users list of favourites

PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:@"Favorites"];
[relation addObject:job];
[user saveInBackground];

then, later, get the favourite jobs

PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:@"Favorites"];
PFQuery *query = [relation query];
[query orderByDescending:@"createdAt"];
query.limit = 200;

[query findObjectsInBackgroundWithBlock:...