使用 PFUser 和 Friendship 限制查询询问

Querying interrogation with PFUser and Friendship restriction

我有我的应用程序,用户可以在其中发表意见,只有他们的朋友才能在时间轴上看到他们并发表评论(在 UITableViewController subclass 中称为 "OpTimelineTableViewController")。但是我有一个关于如何正确执行此限制查询的询问,即使我之前尝试过解决方案没有更多结果......(我开始向您介绍我的数据模型):

"User" table(是Parse自带的class)

"Opinion" table:

objectId;

发件人编号;指针用户 table

内容;字符串

更新时间; (默认提供)

创建于; (默认提供)

"Friends" table:

friendId1 = 指针用户table

friendId2 = 指针用户table

status = String who can take "Confirm", "Pending", or "Denied"

updatedAt =(默认提供)

createdAt =(默认提供)

现在这是我尝试过的代码的主要重要部分:

- (void)loadOpinions {

    PFQuery *opinionQuery = [PFQuery queryWithClassName:@"Opinion"];
    [opinionQuery orderByDescending:@"createdAt"];

    [opinionQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (!error) {

// Array declared as property in the ".h" that contains Opinion objects

            self.opinionArray = [[NSArray alloc] initWithArray:objects];
        }

        [self.tableView reloadData];
        [self.refreshControl endRefreshing];
  }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.opinionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    OpinionTimelineCell *opCell = [tableView dequeueReusableCellWithIdentifier:@"OpTimelineCell" forIndexPath:indexPath];

// self.tempOpObj is an instance of PFObject declared in the ".h" that contains opinion object (for the appropriate index) contained in the opinionArray

    self.tempOpObj = [self.opinionArray objectAtIndex:indexPath.row];

// Now I query "User" table to retrieve all users that have posted Opinions:

    PFQuery *UserOpinionQuery = [PFUser query];

// I do a restriction between users that are contained in the "Opinion" table:

    [UserOpinionQuery whereKey:@"objectId" equalTo:[[self.tempOpObj objectForKey:@"senderId"]objectId]];

    [UserOpinionQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (!error) {

// "userOpInfo" is an instance of PFUser declared in the ".h" that contains the last of object retrieved in "objects":

            self.userOpInfo = [objects lastObject];

            opCell.opinionUsername.text = self.userOpInfo.username;
          }
       }];

    opCell.opinionContent.text = [self.tempOpObj objectForKey:@"content"];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"mm-DD-yyyy";
    opCell.opinionDate.text = [dateFormatter stringFromDate:self.tempOpObj.createdAt];

    [opCell setSelectionStyle:UITableViewCellSelectionStyleNone];

// ? Query to restrict visible Opinion posts only if users are a current user friend:

    PFQuery *restrictFriendQuery = [PFQuery queryWithClassName:@"Friends"];
    [restrictFriendQuery whereKey:@"friendId1" equalTo:[PFUser currentUser]];
    [restrictFriendQuery whereKey:@"friendId2" equalTo:[self.userOpInfo objectId]];
    [restrictFriendQuery whereKey:@"status" equalTo:@"Confirm"];

// Is that way the best ??

    [restrictFriendQuery findObjects];

    return opCell;
}

那时我的问题是:如何通过知道单元格的数量取决于 opinionArray[=66= 中包含的意见的数量来知道我在其他人之前执行的查询],或者简单地说:如何通过限制仅显示来自当前用户的朋友的意见来查询?

(注意:我准确地说我已经实现了方法[self.performSelector(loadOpinions)......在viewDidLoad中获取信息)

我的代码似乎不太清楚我很好,这就是为什么我找不到简单的方法来做到这一点

有没有人可以帮助解决这个问题?

我会使用你的朋友 table 来帮助管理请求和批准流程。我会使用云代码,这样当友谊被批准时,朋友就会被添加到用户 table.

的关系中

通过这样做,您将能够从当前用户那里获得一个包含他们所有朋友的查询。这可以与意见查询一起使用,以将结果限制为朋友用户创建的那些行。