首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将用户添加到PFRelation

将用户添加到PFRelation
EN

Stack Overflow用户
提问于 2014-10-10 05:58:06
回答 1查看 696关注 0票数 0

我试图在我的应用程序中建立一个“好友”系统,当用户接受好友请求时,我将两个用户都添加到一个关系中,如下所示:

代码语言:javascript
复制
var cUserRelation = PFUser.currentUser().relationForKey("friendsRelation") as PFRelation
var senderRelation = sender.relationForKey("friendsRelation") as PFRelation

cUserRelation.addObject(sender)
senderRelation.addObject(PFUser.currentUser())

PFUser.currentUser.saveInBackground()
sender.saveInBackground()

但是由于某些原因,它只为当前用户将发送者添加到关系中。我怎么才能修复它呢?谢谢:)

EN

回答 1

Stack Overflow用户

发布于 2014-10-10 09:20:49

下面是treehouse的一个示例项目,它可能会对您有所帮助:

代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[self.tableView deselectRowAtIndexPath:indexPath animated:nil];


UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

// if the current relationKey doesn't exist, it will be create for us.
PFRelation *friendsRelation = [self.currentUser relationForKey:@"friendsRelation"];
// adding users is easy. use addObject methoed then we can pass in the user that was tapped on as the parameter. To get that user, we have to get the indexPath just like we did in the 'cellForRowAtIndexPath' method
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];

if ([self isFriend:user])
{

    cell.accessoryType = UITableViewCellAccessoryNone;
    // 2. remove from the array of friends // the only way we have to find a user in an array is to loop through and look for a matching objectid.
    for (PFUser *friend in self.friends)
    {
        if ([friend.objectId isEqualToString:user.objectId])
        {
            [self.friends removeObject:friend];
            break;
        }
    // 3. Remove from the backend
        [friendsRelation removeObject:user];
        [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }];
    }
}
    else
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.friends addObject:user];
    for (PFUser *friend in self.friends) {
        if ([friend.objectId isEqualToString:user.objectId]) {
            [self.friends addObject:friend];
            break;
        }

        // and now we can use the user variable here as the parameter for addObject, which adds the user locally
        [friendsRelation addObject:user];
        // but we also have to add the user on the backend
        [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (error) {
                NSLog(@"Error %@ %@", error, [error userInfo]);
            }
        }];


    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26288592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档