使用混合对象的iOS Parse查询

时间:2015-10-21 12:20:06

标签: ios swift parse-platform relational-database

我有一个与Parse.com合作的聊天应用程序。

在我的应用用户中可以添加帖子。喜欢按钮喜欢和正常状态。因此,下次当用户打开应用程序时,我应该显示用户喜欢按钮状态的帖子。

服务器对象: 1)用户 2)发布 3)喜欢

用户对象列: 1.喜欢(关系) - 所有用户喜欢的帖子 2.帖子(关系) - 所有用户发布的帖子

发布对象列: 1.用户(指针) - 由用户指针创建 2.喜欢(关系) - 帖子上所有现有的喜欢

与对象列类似: 1.用户(指针) - 用户喜欢 2.帖子(指针) - 喜欢帖子

/ ****问题**** / 现在我收到来自"帖子"的所有帖子。表格,然后从"用户的喜欢帖子关系中获取所有喜欢的帖子"如果帖子表中的帖子存在于"用户的喜欢帖子关系"我将Like按钮状态更改为Liked。

我不喜欢我现在这样做,因为我现在要求两个显示帖子的请求。按钮状态正确。我确信单个请求可以做到,但不知道如何做。

有人可以帮我吗???

1 个答案:

答案 0 :(得分:0)

我认为您不需要使用User类中的任何数据来推断出您想要的内容。您只需在单个查询中获取每个Post对象的关联Likes数据。然后处理每个的Likes信息,看看当前用户是否在那里设置Like按钮状态:

// Get all the posts
let postQuery = PFQuery(className: "Post")

// Include all like objects to for each post as well
postQuery.includeKey("Likes")

// execute the query
postQuery.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]?, error: NSError?) -> Void in

    // objects are all of the Post objects, and their associated Like objects, too
    // Process data for each post and check the likes info to see if your current 
    // user is there, then set the Like button accordingly
}