如何为一个解析类建模以包括islike,isfollowing字段以及PFQuery结果 [英] how to model a parse class to include isliked, isfollowing fields along with the PFQuery results

查看:66
本文介绍了如何为一个解析类建模以包括islike,isfollowing字段以及PFQuery结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到查询Posts(PFObject)或Users(PFUser)类的有效方法,并且在结果数组中分别包含isPostLiked(boolean)和isUserFollowed(boolean).

I can't find an efficient way to query Posts(PFObject) or Users(PFUser) classes and also have the isPostLiked(boolean) and isUserFollowed(boolean) included in the results array respectively.

可以说,我已经查询并收到了来自服务器的25篇帖子.如果我以前喜欢此帖子,我想用红色填充喜欢的心"按钮.查询这些帖子的所有赞,并查看结果中是否包含当前用户,将是非常低效的.

Lets say, I have queried and received 25 Posts from the server. I want to fill in the like heart button with red if I have previously liked this Post. It would be very inefficient to query all the likes of these Posts and see if current user is contained in the results.

是否可以编写云代码函数以在查询结果中插入"isLiked"字段并将其返回给用户(例如)?

Is it possible to write a cloud code function to insert an 'isLiked' field to the query results and return it to the User for instance?

自从我被困在这里以来,我愿意接受新的策略.显而易见,大多数社交应用程序都将这种需求作为标准,因此必须有一个有效的解决方案.谢谢

I am open to new strategies since I am stuck here. It is obvious that most of the social apps are having this need as a standard so there must be an effective solution. Thanks

推荐答案

您最好的行动是摆脱关系数据库的思维.在我看来,您有一个单独的Likes类,该类跟踪哪个用户喜欢哪个帖子.

Your best action is to rid yourself of the relational database thinking. It seems to me you have a separate Likes class that tracks which user likes which post.

在NoSQL空间中,当您计划数据模型时,应重点关注查询.问问自己这个问题:

In the NoSQL space you should focus on your queries when you plan your datamodel. Ask yourself this question:

我要如何查询我的数据?

How do I want to query my data?

在这种情况下,我认为您可能想要

In this use case, I'm thinking you might want to

  • 显示帖子喜欢多少人
  • 也许显示哪些用户喜欢该帖子
  • 跟踪当前用户是否喜欢某个帖子
  • 也许找到当前用户喜欢的所有帖子?

要解决此问题,我将执行以下操作:

To solve this, I would do the following:

  • 在Post类上,添加一列 likedby .
  • 在User类上,添加一列 likedposts . 这两个列都是数组列
  • On the Post class, add a column likedby.
  • On the User class, add a column likedposts. Both these columns are Array columns

每次用户喜欢帖子时,都将指向当前用户的Pointer添加到Post的 likedby 数组列中,并指向指向 likedposts 的帖子的指针用户的数组列.

Every time a user likes a post, you add a Pointer to the current user to the likedby array column for the Post AND a pointer to the post to the likedposts array column for the User.

这很容易

  • 查找帖子的喜欢人数( liked 中的元素数量)
  • 列出所有喜欢该帖子的用户(使用帖子上的query.includeKey("likedby"))
  • 检查当前用户是否已经喜欢该帖子(如果数组所点赞的用户包含currentuser)
  • 列出用户喜欢的所有帖子(使用用户上的query.includeKey("likedposts")).
  • find how many likes a post has (number of elements in likedby)
  • list all the users that liked the post (using query.includeKey("likedby") on the Post)
  • check if the current user has already liked the post (if likedby array contains currentuser)
  • list all the posts a user has liked (using query.includeKey("likedposts") on the User).

对以下内容使用相同的逻辑.

Use the same logic for followings.

这篇关于如何为一个解析类建模以包括islike,isfollowing字段以及PFQuery结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆