我对ios编程很新,并且要解析,所以如果我没有明确解释某些内容,我会非常乐意尽力扩展它。
在解析中我有三个表User
Activity
和post
我现在的代码很适合“跟随”关系,但不适合“喜欢”关系,我想要从Post
表获取对象id,并从User
表中获取喜欢帖子的用户的对象ID,并将它们插入Activity
表。任何帮助将不胜感激,谢谢你提前。
- (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];
}
}
}];
}
答案 0 :(得分:0)
好的,首先我会提到你应该非常小心区分大小写。 Parse中的字符串匹配区分大小写,因此如果您使用type:@"Like"
创建它,然后尝试使用whereKey:@"type" equalTo:@"like"
找到它,那么您将无法获得匹配!
现在,对于您的代码,您想要将当前用户链接到所选帖子吗?
如果是这样,那么你应该做些什么的样本:
- (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];
}
您的代码似乎是从您的"关注"中复制粘贴的。逻辑,但没有更新,以反映你现在正在做"喜欢"的事实,所以我已经为你解决了这个问题。您还需要对unlikePost:
方法进行类似的更改。