通过向用户添加收藏夹指针来更新解析类

时间:2015-06-25 03:54:07

标签: ios pointers parse-platform favorites

我正在尝试为我的应用添加新功能,该应用程序功能来自世界各地的工作。目前,用户可以点击标签栏按钮并查看可供选择的作业列表。从该表视图控制器,用户可以选择通过点击表格视图单元来查看作业,该表格视图单元格将显示多个文本字段和解释作业的文本视图的详细视图。

我添加了一个名为Favorites的新视图,这样如果用户在详细视图上点击一个星号,它就会将PFUser添加到该特定作业的名为Favorites的parse中的指针列。

但是,这不按计划进行。相反,它在我的解析类中创建一个新的Job,除了指针列之外,所有字段都是空白的。我的问题是如何将特定用户与他们正在查看的作业相关联,以便我稍后可以在我的应用的“收藏夹”标签中将其提取出来?

我的解析类数据包含以下内容:

"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 个答案:

答案 0 :(得分:0)

您可能应该使用从User到Job的关系,而不是使用指针或指针数组。然后,当点击按钮时,您可以将喜好添加(或删除)到关系中并保存用户。

你致电objectWithClassName的地方你正在创建一个新的工作实例,这不是你想要的。您应该引用您下载的实例来填充UI。但是,如果你这样做,任何工作只能被一个用户所青睐。此外,除了创建它的人之外,这个工作应该只对每个人都是只读的。

所以,改为使用关系。

将作业添加到收藏夹的用户列表

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

然后,获得最喜欢的工作

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

[query findObjectsInBackgroundWithBlock:...