Reputation: 9364
I'm facing an issue with Parse.
I have three classes : Users, Posts, Likes
I'm using pointers for the Likes class User pointing to Users Class, and Post pointing to Posts class.
I'm retrieving all the posts and then I would like to see if the current user likes a post or not, and for that I'm nesting a query. the problem I have is that the posts are not updated with the like boolean value. I'm using that code :
func loaddata(limit:Int, skip:Int) {
MainFunctions.getphones{(phones) -> Void in
var indxesPath:[NSIndexPath] = [NSIndexPath]()
let query = PFQuery(className: "Feed")
query.whereKey("username", containedIn: phones)
query.limit = limit
query.skip = skip
query.includeKey("from")
query.addDescendingOrder("createdAt")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
if objects.isEmpty {
}
else {
for object in objects {
let Date = object.createdAt
let post = Post(time: Date!)
let Type = object.objectForKey("type") as? String
post.Post = object
post.Post_message = object.objectForKey("Text") as? String
post.comments = object.objectForKey("commentaires") as? Int
post.likes = object.objectForKey("likes") as? Int
// See if the current user likes a post
let query_like = PFQuery(className:"Likes")
query_like.whereKey("Post", equalTo: object)
query_like.whereKey("User", equalTo: PFUser.currentUser()!)
query_like.getFirstObjectInBackgroundWithBlock{
(like: PFObject?, error: NSError?) -> Void in
if error == nil && like != nil {
post.DoILike = true
} else {
post.DoILike = false
}
}
self.posts.append(post)
indxesPath.append(NSIndexPath(forRow: self.posts.count - 1, inSection: 0))
}
self.tableView.beginUpdates()
self.tableView.insertRowsAtIndexPaths(indxesPath, withRowAnimation: UITableViewRowAnimation.Bottom)
self.tableView.endUpdates()
self.tableView.finishInfiniteScroll()
self.refreshControl.endRefreshing()
}
}
} else {
self.tableView.finishInfiniteScroll()
self.refreshControl.endRefreshing()
}
}
}
}
Upvotes: 0
Views: 57
Reputation: 3337
query_like.getFirstObjectInBackgroundWithBlock
is asynchrone so when you are adding the post to the posts array self.posts.append(post)
post.DoILike
will get it's default value. So make sure when appending post to posts you have already get if the user liked the post or not.
Upvotes: 1