2015-05-25 54 views
0

我試圖將當前用戶的用戶名保存到與另一個用戶關聯的數組,但ACL不允許將數據寫入另一個用戶對象。該代碼讓您輸入用戶名,如果用戶存在,它會將該用戶名添加到您關注的用戶數組中。當您開始關注用戶時,您的用戶名需要添加到您剛剛關注的人的關注者陣列中。我怎樣才能做到這一點?將數據保存到另一個用戶對象

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     UITextField *alertTextField = [alertView textFieldAtIndex:0]; 
     self.username = alertTextField.text; 

     PFQuery *query = [PFUser query]; 
     [query whereKey:@"username" equalTo:self.username]; 
     PFUser *user = (PFUser *)[query getFirstObject]; 

     NSLog(@"User: %@", user[@"username"]); 

     if (!(user[@"username"] == NULL)) { 
      if (![self.followersList containsObject:user[@"username"]]) { 
       [self.followersList addObject:user[@"username"]]; 
       [[PFUser currentUser]setObject:self.followersList forKey:@"Following"]; 
       [[PFUser currentUser]saveInBackground]; 

       NSMutableArray *otherUsersFollowers; 

       if ([user objectForKey:@"Followers"] == NULL) { 
        otherUsersFollowers = [[NSMutableArray alloc]init]; 
       } 
       else { 
        otherUsersFollowers = [user objectForKey:@"Followers"]; 
       } 

       [otherUsersFollowers addObject:[PFUser currentUser].username]; 
       [user setObject:otherUsersFollowers forKey:@"Followers"]; 
       [user saveInBackground]; 

       [self.tableView reloadData]; 
      } 
      else { 
       UIAlertView *alreadyFollow = [[UIAlertView alloc]initWithTitle:@"" message:@"You already follow that user." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
       [alreadyFollow show]; 
      } 
     } 
     else { 
      UIAlertView *noUserAlert = [[UIAlertView alloc]initWithTitle:@"" message:@"That user does not exist" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      [noUserAlert show]; 
     } 
    } 
} 

回答

1

您需要使用帶有主密鑰的雲代碼。主密鑰允許您繞過類級權限和ACL。在您的iOS代碼中調用你的雲功能:

NSDictionary *params = @{@"otherUserId": @(user.objectId), @"username": [PFUser currentUser].username}; 
[PFCloud callFunctionInBackground:@"addFollower" withParameters:params block:^(id object, NSError *error) { 
    // Probably you want to reload your table here  
}]; 

雲功能,可以像:

function addFollower (request, response) { 
    var user = request.user; 
    var otherUserId = request.params.otherUserId; 
    var username = request.params.username; 
    if (!user) { 
     response.error("Need to login"); 
     return; 
    } else if (!otherUserId) { 
     response.error("Need the other user's id"); 
     return; 
    } else if (!username) { 
     response.error("Need the current user's username"); 
     return; 
    } 

    var otherUser = Parse.User.createWithoutData(otherUserId); 
    otherUser.addUnique("followers", username); 
    otherUser.save(null, {useMasterKey:true}).then(function (otherUser) { 
     response.success(); 
    }, function (error) { 
     response.error(error); 
    }) 
} 

Parse.Cloud.define("addFollower", addFollower); 

請注意,我用saveuseMasterKey:true選項,這樣的ACL將在這個特殊的繞過保存。相反,您也可以在該函數的開始處添加一行Parse.Cloud.useMasterKey();,這將允許您在函數內的所有操作中跳過ACL。更多信息在the docs

您可能還想將更新當前用戶關注的部分移到此雲代碼函數中。

+0

這個工作除了它創建一個名爲「追隨者」的新列。我已經有一個名爲「追隨者」的數組列,我想用我的otherUsersFollowers填充NSMutableArray – raginggoat

+0

沒關係。 「追隨者」只需要大寫。 – raginggoat

0

要麼不和運行查詢來發現所有的追隨者或做它在雲中的代碼,你可以使用萬能鑰匙。

相關問題