我正在使用Parse和我正在构建社交网络应用,用户可以对帖子发表评论,而其他用户可能会像Facebook App一样喜欢该帖子。
这是我的一段代码
override func viewDidLoad() {
super.viewDidLoad()
// Get all comments
var query = PFQuery(className: "Comment")
query.findObjectsInBackgroundWithBlock { comments, error in
if error == nil {
self.comments = comments
}
}
}
override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let commentForThisRow = self.comments[indexPath.row] // contains array of PFObject
// Get all activities which like this comment
var likeQuery = PFQuery(className: "Activity")
likeQuery.whereKey(kSFActivityType, equalTo: kSFActivityTypeLike)
likeQuery.whereKey(kFSActivityComment, equalTo: commentForThisRow)
likeQuery.findObjectsInBackground {
// Store array of users who like this comment to the cell
}
}
问题是,因为每次用户滚动tableView时调用indexpath的行的单元格,它会使likeQuery每次运行并执行请求。
如何使likeQuery请求只运行一次?