Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 解析关系数据的查询没有使用orderedByAscending按顺序返回_Ios_Relational Database_Parse Platform_Pfquery - Fatal编程技术网

Ios 解析关系数据的查询没有使用orderedByAscending按顺序返回

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

我正在查询parse上的关系数据,我希望对象按照创建日期的顺序返回。我以前使用过这种方法,但无法使用关系数据获得有序查询。查询返回的顺序是随机的。提前谢谢!这是我的密码:

    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; 
     }

我对你的代码有点困惑

  • 您创建了一个“postQuery”,并调用它,但从不使用它的任何数据
  • 还有一个roomQuery似乎从未被分配或使用过
  • 您正在按其名称查询特定帖子。你在控制它的名字吗?如果没有,您应该使用id
  • 什么是PFRelation commentsRelation=[@“Comments”] 可能是因为它只是一个片段,这些东西是在别处处理的;但是,对于我的答案,我假设您的“comments”字段是一个“Comment”类对象数组

    备选案文1:

    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
    }];