ios - 连接表 parse.com

标签 ios parse-platform

我有两个解析表 POST 和 COMMENT。 我想获得所有有新评论的帖子。我正在根据帖子表中的 viewedDate 字段和评论表中的 createdAt 字段进行检查。

SELECT *
FROM POST p
INNER JOIN COMMENT c
ON p.objectId==c.pId and c.createdAt > p.viewedDate;

PFQuery *post = [ParsePost query];


[post findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    PFQuery *post = [ParsePost query];

    PFQuery *comment = [ParseComment query];
    //how add greater than for keys from other table?
    //line below crashes
    [comment whereKey:@"createdAt" greaterThan:[objects valueForKey:@"viewedDate"]];

    [comment whereKey:@"post" matchesKey:@"objectId" inQuery:post];


    [post whereKey:@"objectId" matchesKey:@"post" inQuery:comment];



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

    }];

}];

更新 *这就是我目前的做法*

PFQuery *postView = [ParsePost query];
[postView whereKey:@"author" equalTo:[ParseLigoUser currentUser]];

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


    for (ParsePost *post in objects) {
        PFQuery *comment = [ParseComment query];
        [comment orderByDescending:@"createdAt"];
        if (post.viewedDate) {
            [comment whereKey:@"createdAt" greaterThan:post.viewedDate];

        }else {
            continue;
        }
        [comment whereKey:@"post" equalTo:post];
         [comment countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
             if (number>0) {
                 if (!self.newposts) {
                     self.newposts = [NSMutableDictionary dictionary];
                 }
                 [self.newposts setObject:@"NOTSEEN" forKey:[post.objectId stringByAppendingString:@"Comment"]];

                 self.postCount += 1;

             }
        }];

    }
}];

更新 2 关系

PFQuery *postView = [ParsePost query];
[postView whereKey:@"author" equalTo:[ParseLigoUser currentUser]];
[postView setLimit:100];
[postView findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    for (ParsePost *post in objects) {
    PFRelation *rel = [post relationForKey:@"hasComment"];

        PFQuery *query = [rel    query];
        [query orderByDescending:@"createdAt"];

        if (post.viewedDate) {
            [query whereKey:@"createdAt" greaterThan:post.viewedDate];

        }else {
            continue;
        }




        [[rel query] countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
            if (number>0) {
                if (!self.newposts) {
                    self.newposts = [NSMutableDictionary dictionary];
                }
                [self.newposts setObject:@"NOTSEEN" forKey:[post.objectId stringByAppendingString:@"Comment"]];
                self.postCount += 1;
            }
        }];
    }
}];

最佳答案

你的问题并不是真正缺乏解析时的连接表查询,而是你用 sql 思维方式而不是 NoSQL 思维方式设计你的数据库(这对于所有来自关系数据库世界的人来说当然很常见) .

在为解析(或任何其他 NoSQL 数据库)设计数据库“模式”时,您需要将自己从关系和规范化等方面的思考中解放出来,转而思考数据访问。您将如何访问您的数据?首先考虑您将如何查询数据,然后设计您的数据库以针对这些查询进行优化。对于您的移动应用来说,最重要的是尽量减少与远程服务器的连接数量,并尽量减少客户端处理。

有几种方法可以解决您当前的问题。当然,您可以创建一些查询来解决缺少连接表查询的问题,并在客户端处理其中的一些问题。如果需要快速实现此功能,这可能会在短期内奏效。

一个长期的方法是重新设计您的架构以满足您轻松检索具有新评论的帖子的要求。

一种解决方案(几种可能的):

在 Post 类中创建一个新属性: bool 值“newcomments”。

创建一个云代码片段,每当创建新评论时更新 Post 中的 newcomments 属性(将其设置为 TRUE)。 此代码段应在评论的 afterSave Hook 中运行(https://parse.com/docs/cloud_code_guide#functions-aftersave)

然后,每当您打开帖子查看新评论时,您都会在后台将此字段重置为 FALSE。 现在您可以查询 newcomments 等于 false 的帖子

或者,您也可以使用它来存储指向实际新评论的指针数组,而不是 newcomments 是一个 bool 值(afterSave Hook 使用指向新评论的指针更新该数组)。这样一来,打开帖子后就不需要第二次查询来获取新评论。在这里,您在阅读评论(或打开帖子并获得评论数组)后立即清除 newcomments 属性。

这种存储数组的方式可能会在您的 SQL 思维方式中留下不好的印象,但这是 SQL 和 NoSQL 之间的众多差异之一,因为后者更注重查询效率而不是存储和一致性。

或者,如果您不想将它存储在 Post 对象中,您可以创建一个新的 PostTracker(或其他)类来处理它。也许还有其他您想要跟踪的东西(当然,将来可能会有,即使您目前还没有这方面的想法)。

关于ios - 连接表 parse.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22396845/

相关文章:

javascript - Parse.com 云代码 - 保存后

ios - 错误 ITMS-90596 : "Invalid Bundle. The asset catalog at ' Payload/Limon chilli. app/Assets.car' 无法读取

ios - 如何在 RxSwift 中使用数据源和委托(delegate)方法

ios - Mailgun:使用解析云代码发送图像

parse-platform - 如何响应 Parse Cloud Function 中的错误并防止重试?

swift - 在 swift 中将来自不同类的两个查询合并为一个查询

javascript - 如何在 Parse-SDK-JS 中创建具有关系的新 Parse.Object

ios - 如何在 Swift 中禁用 "Join Wi-Fi network"警报消息?

ios - 快速向背景添加模糊效果

ios - UITableView :reloadSectionIndexTitles animated correct way