在解析中使用 "like" 关系的指针

Using pointers for a "like" relationship in parse

我对 ios 编程和解析还很陌生,所以如果我没有清楚地解释某些事情,我会非常乐意尽我最大的努力对此进行扩展。

在解析中我有三个 tables User Activitypost 我现在拥有的代码适用于 "following" 关系但不对于 "like" 关系,我想从 Post table 中获取对象 ID,并从 [=12= 中获取喜欢 post 的用户的对象 ID ] table,并将它们插入Activity Table。任何帮助将不胜感激,在此先感谢您。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
if (indexPath.section == self.objects.count && self.paginationEnabled) {
    [self loadNextPage];
    }
}

 - (PFQuery *)queryForTable
{
 PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
 [query includeKey:@"User"];
 query.cachePolicy = kPFCachePolicyCacheThenNetwork;
 [query whereKey:@"filter" hasPrefix:@"90"];
 [query orderByDescending:@"createdAt"];


    return query;

}




 - (void)likeButton:(LikeButton *)button didTapWithSectionIndex:(NSInteger)index {
PFObject *post = [self.objects objectAtIndex:index];
[post fetchIfNeeded];
PFUser *user = post[@"User"];

if (!button.selected) {
    [self likePost:user];
}
else {
    [self unlikePost:user];
}
    [self.tableView reloadData];
}





 - (void)likePost:(PFUser *)user {
if (![user.objectId isEqualToString:[PFUser currentUser].objectId]) {
    [self.likeArray addObject:user.objectId];
    PFObject *likeActivity = [PFObject objectWithClassName:@"Activity"];
    likeActivity[@"fromUser"] = [PFUser currentUser];
    likeActivity[@"toPost"] = user;
    likeActivity[@"type"] = @"Like";
    [likeActivity saveEventually];
   }
}
- (void)unlikePost:(PFUser *)user {
[self.likeArray removeObject:user.objectId];
PFQuery *query = [PFQuery queryWithClassName:@"Activity"];
[query whereKey:@"fromUser" equalTo:[PFUser currentUser]];
[query whereKey:@"toPost" equalTo:user];
[query whereKey:@"type" equalTo:@"like"];
[query findObjectsInBackgroundWithBlock:^(NSArray *likeActivities, NSError *error) {
    if (!error) {
        for (PFObject *likeActivity in likeActivities) {
            [likeActivity deleteEventually];
        }
    }

    }];
} 

好的,首先我要提一下,您应该非常小心区分大小写。 Parse 中的字符串匹配区分大小写,因此如果您使用 type:@"Like" 创建它然后尝试使用 whereKey:@"type" equalTo:@"like" 找到它,您将无法匹配!

现在,至于你的代码,你想link当前用户选择post对吗?

如果是这样,下面是您应该做的示例:

- (void)likeButton:(LikeButton *)button didTapWithSectionIndex:(NSInteger)index {
    PFObject *post = [self.objects objectAtIndex:index];

    if (!button.selected) {
        [self likePost:post];
    }
    else {
        [self unlikePost:post];
    }

- (void)likePost:(PFObject *)post {
    [self.likeArray addObject:user.objectId];
    PFObject *likeActivity = [PFObject objectWithClassName:@"Activity"];
    likeActivity[@"fromUser"] = [PFUser currentUser];
    likeActivity[@"toPost"] = post;
    likeActivity[@"type"] = @"like";
    [likeActivity saveEventually];
}

看起来您的代码是从您的 "follow" 逻辑中复制粘贴的,但没有更新以反映您现在正在执行 "like" 的事实,所以我已经为您解决了这个问题。您也需要对 unlikePost: 方法进行类似的更改。