0

I am having trouble saving a PFRelation in Parse. I have a message PFObject and want to add this to a PFRelation receivedMessages in the PFUser class. Code is below. Everything works up to actually trying to create the relationship. I have set the user object fine, NSLog displays the correct user but it appears [sendtoUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) is not being called at all as "relation saved" is not displayed in the console. Could someone help please? Thanks

PFObject *message = [PFObject objectWithClassName:@“message”];
message[@“fromUser"]=[PFUser currentUser].username;
message[@“message"]=@"喵!";
message[@“read"]=@NO;

[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

    NSLog(@“message saved”);
    PFQuery *usernameQuery = [PFUser query];
    [usernameQuery whereKey:@"username" equalTo:[[self.friends objectAtIndex:index] username]];
    [usernameQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        PFUser *sendtoUser = (PFUser *)objects[0];
        NSLog(@"send to user: %@", sendtoUser.username); //this works

        PFRelation *receivedMessages = [sendtoUser relationForKey:@"receivedMessages”];
        [receivedMessages addObject:message];

        [sendtoUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            NSLog(@“relation saved”); //this doesn’t display at all                
        }];//end save user relation block

    }];//end username query block


}];//end save message block, this works
Kex
  • 8,023
  • 9
  • 56
  • 129
  • I also checked the PFRelation with NSLog("%@".receivedMessages); and all seems in order. It's just that final line when I try to save it, I can't figure out for the life of me why it won't work. – Kex Jan 22 '15 at 11:01
  • what is (PFUser *)objects[0] returning? – soulshined Jan 22 '15 at 16:45
  • also is `equalTo:[[self.friends objectAtIndex:index] username]]` valid? usually index isn't a valid indexPath unless you've named an NSIndexPath index? – soulshined Jan 22 '15 at 17:01
  • I figured it out, you cannot modify another PFUsers fields, you can only modify your own. Had to solve it by creating two new classes, messages and messageBank. The message bank is linked to the PFUser and message bank has a PFRelation to messages. Can save info that way. thanks – Kex Jan 22 '15 at 17:15
  • this is true - pointers are the key to successfully cross-referencing in Parse – soulshined Jan 22 '15 at 17:18

0 Answers0