我有一个使用 Parse.com 的聊天应用程序。
在我的应用程序中,用户可以点赞帖子。喜欢按钮有喜欢和正常状态。因此,下次当用户打开应用程序时,我应该显示用户喜欢的帖子以及喜欢的按钮状态。
服务器对象: 1)用户 2) 邮寄 3)喜欢
用户对象列: 1. Likes(relation) - 用户所有喜欢的帖子 2. Posts(relation) - 所有用户发布的帖子
发布对象列: 1. Users(pointer) - 由用户指针创建 2. 点赞(关系)- 帖子中所有现有的点赞
喜欢的对象列: 1. User(pointer) - 用户点赞 2. 帖子(指针)- 喜欢的帖子
/**** 问题 ****/ 现在我从“帖子”表中获取所有帖子,然后从“用户喜欢的帖子关系”中获取所有喜欢的帖子,如果帖子表中的帖子存在于“用户喜欢的帖子关系”中,我正在更改喜欢按钮状态到喜欢。
我不喜欢我现在的做法,因为我正在调用两个请求来显示“赞”按钮状态正确的帖子。我确信可以处理单个请求,但不知道怎么做。
有人可以帮帮我吗???
最佳答案
我认为您不需要使用 User 类中的任何数据来推断您想要的内容。您可以在单个查询中简单地获取每个 Post 对象的关联 Likes 数据。然后处理每一个的点赞信息,看当前用户是否在那里设置点赞按钮状态:
// 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
}
关于具有混合对象的 iOS 解析查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33259163/