Ios 解析关系数据的查询没有使用orderedByAscending按顺序返回
我正在查询parse上的关系数据,我希望对象按照创建日期的顺序返回。我以前使用过这种方法,但无法使用关系数据获得有序查询。查询返回的顺序是随机的。提前谢谢!这是我的密码:Ios 解析关系数据的查询没有使用orderedByAscending按顺序返回,ios,relational-database,parse-platform,pfquery,Ios,Relational Database,Parse Platform,Pfquery,我正在查询parse上的关系数据,我希望对象按照创建日期的顺序返回。我以前使用过这种方法,但无法使用关系数据获得有序查询。查询返回的顺序是随机的。提前谢谢!这是我的密码: PFQuery *postQuery = [PFQuery queryWithClassName:@"Post"]; [roomQuery whereKey:@"name" equalTo:self.postName]; NSError *error; //done on main threa
PFQuery *postQuery = [PFQuery queryWithClassName:@"Post"];
[roomQuery whereKey:@"name" equalTo:self.postName];
NSError *error;
//done on main thread to have data for next query
NSArray *results = [postQuery findObjects:&error];
PFObject *post;
if ([results count]) {
post = [results objectAtIndex:0];
NSLog(@"results were found");
} else {
NSLog(@"results were not found");
}
PFRelation *commentsRelation = [@"Comments"];
[commentsRelation.query orderByAscending:@"createdAt"];
[commentsRelation.query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error Fetching Comments: %@", error);
} else {
NSArray *comments = objects;
}
我对你的代码有点困惑
PFQuery * postQuery = [PFQuery queryWithClassName:@"Post"];
[postQuery whereKey:@"name" equalTo:self.postName];
// again, possibly an id field would be more reliable
// [postQuery whereKey:@"objectId" equalTo:self.postId];
[postQuery includeKey:@"Comments"];
PFObject * post = [postQuery getFirstObject];// no need to download all if you just want object at [0]
// this will contain your post and all of it's comments with only one api call
// unfortunately, it's not sorted, so you would have to run a sort.
NSArray * comments = [post[@"Comments"] sortedArrayUsingComparator: ^(id obj1, id obj2) {
return [obj1[@"createdAt" compare: obj2[@"createdAt"];
}];
备选案文2:
也许更好的选择是修改您的数据结构,而不是将评论与文章相关联,您可以将文章与评论相关联(如在解析文档中)
上述两种解决方案都避免了进行额外的不必要的api调用(毕竟是基于调用的解析费用!)。只要阅读代码,我就看不出有任何错误。你能给我们看一个注释中的数据示例吗?“PFRelation*commentsRelation=[@“comments”];”这段代码甚至不应该编译,你能修复它以显示如何获得注释关系吗?
PFQuery * postQuery = [PFQuery queryWithClassName:@"Post"];
[postQuery whereKey:@"name" equalTo:self.postName];
// again, possibly an id field would be more reliable
// [postQuery whereKey:@"objectId" equalTo:self.postId];
PFQuery * commentQuery = [PFQuery queryWithClassName:@"Comment"];
[commentsQuery whereKey:@"parent" matchesQuery:postQuery]; // when creating a comment, set your post as its parent
[commentsQuery addOrderDescending:@"createdAt"]
[commentQuery findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
// comments now contains the comments for myPost
}];