我有我的应用程序,用户可以在其中撰写意见,只有他们的朋友可以在时间线中看到它们并对其进行评论(在名为"OpTimelineTableViewController"的UITableViewController子类中)。但是我有一个关于如何正确执行此限制查询的询问,即使我之前尝试过没有更多结果的解决方案......(我开始向您介绍我的数据模型):
“用户”表(是 Parse 提供的内置类)
“意见”表:
对象标识;
发件人ID; 指针用户表
内容; 细绳
更新时间;(默认提供)
创建时间;(默认提供)
“朋友”表:
friendId1 = 指针用户表
friendId2 = 指针用户表
status = 可以接受“Confirm”、“Pending”或“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中包含的意见的数量,或者只是简单地:如何通过仅限制显示的意见来查询当前用户的朋友?
(注意:我确切地说我已经在 viewDidLoad 中实现了方法 [self.performSelector(loadOpinions)....... 以获取信息)
我的代码似乎不清楚我一切都好,这就是为什么我找不到简单的方法来做到这一点
有没有人可以帮助解决这个问题?