1
我成功添加好友Cloud代碼和Parse.com。使用Cloud Code刪除PFUser Parse.com iOS
現在我想用雲代碼以刪除didSelectRowAtIndexPath方法的朋友關係
我的錯誤是「attempt to insert nil object from objects[0]'
」
但我不知道我需要配置什麼參數,我發現爲雲代碼main.js:
Parse.Cloud.define("removeFriend", function(request, response)
{
// here's how to get the client's user making the request
var user = request.user;
// consider checking that user && request.params.friend are valid
// if not, return response.error("missing user or friend id")
getUser(request.params.friend).then(function(friend) {
// your code prematurely called response.success() here, thereby canceling any further steps
friend.relation("friendsRelation").remove(user);
// return the promise returned by save() so we can chain the promises
return friend.save();
}).then(function(result) {
// only now that the save is finished, we can claim victory
response.success(result);
}, function (error) {
response.error(result);
});
});
// EDIT - the OP once referred to a getUser function that we assume to be something like this:
// return a promise to get a user with userId
function getUser(userId) {
var userQuery = new Parse.Query(Parse.User);
return userQuery.get(userId);
}
這裏是我的代碼艾迪tFriends.m:
- (void)viewDidLoad
{
[super viewDidLoad];
PFQuery *query = [PFUser query];
[query orderByAscending:@"name"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
self.allUsers = objects;
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
}];
self.currentUser = [PFUser currentUser];
[self loadFriends];
}
-(void) loadFriends{
self.friendsRelation = [[PFUser currentUser] objectForKey:@"friends"];
PFQuery *query = [self.friendsRelation query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
else {
self.friends = objects;
[self.tableView reloadData];
}
}];
}
- (BOOL)isFriend:(PFUser *)user {
for(PFUser *friend in self.friends) {
if ([friend.objectId isEqualToString:user.objectId]) {
return YES;
}
}
return NO;
}
的cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
NSString *name = [[self.allUsers objectAtIndex:indexPath.row] valueForKey:@"username"];
cell.textLabel.text = name;
if ([self isFriend:user]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
didSelectRowAtIndexPath方法:
PFUser *selected = [self.allUsers objectAtIndex:indexPath.row];
if ([self isFriend:selected]) {
NSLog(@"déjà amis");
// PFObject *friendRequest = [self.friendRequests objectAtIndex:indexPath.row];
[PFCloud callFunctionInBackground:@"removeFriend" withParameters:@{@"friendRequest" : selected.objectId} block:^(id object, NSError *error) {
if (!error) {
//add the fromuser to the currentUsers friends
//save the current user
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
} else {
}
}];
}
else {
}
}];
}
else{
PFUser *selectedUser = [self.allUsers objectAtIndex:indexPath.row];
//request them
PFObject *friendRequest = [PFObject objectWithClassName:@"FriendRequest"];
friendRequest[@"from"] = self.currentUser;
friendRequest[@"fromUsername"] = [[PFUser currentUser] objectForKey:@"username"];
//selected user is the user at the cell that was selected
friendRequest[@"to"] = selectedUser;
// set the initial status to pending
friendRequest[@"status"] = @"pending";
[friendRequest saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yay" message:@"Friend request sent" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
} else {
// error occurred
}
}];
}
嘿,感謝回覆的人!我想我不會發送正確的用戶objectId,因爲我的應用程序再次崩潰:[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:試圖從對象[0]插入nil對象'...所以有些對象是零,我不不知道我做錯了什麼,我沒有更改我的iOS代碼。 –
我成功通過以下代碼獲取didSelectRowAtIndexPath中的用戶objectId:PFUser * user = [self.allUsers objectAtIndex:indexPath.row]; NSString * objectId = [user objectId];但接下來我該怎麼辦?非常感謝! –
這是因爲friendRequest是零,但我不知道如何解決它,我需要什麼參數...我更新了我的帖子與截圖從解析 –