I am working on a social media app where I have implemented basic like/unlike/follow/unfollow features. I am using Parse as a backend. Here is what happens.
I am getting the posts liked by the user from my backend with this function in my ViewDidLoad, this seems to be working:
func getLikes() {
var query = PFQuery(className: "Likes")
query.whereKey("Liker", equalTo: PFUser.currentUser()["displayName"])
query.findObjectsInBackgroundWithBlock { (results:[AnyObject]!, error:NSError!) -> Void in
if error == nil {
for result in results {
self.likedPosts.append(result["likedItem"] as String)
}
println(self.likedPosts)
self.homeTableView.reloadData()
}
}
}
Then, in my cellForRowAtIndexPath, I set the title and the function of the likeButton according to whether or not the id of the post for that cell is contained in the array of liked posts:
if contains(self.likedPosts, self.id[indexPath.row]) {
cell.thankButton.setTitle("Unlike", forState: UIControlState.Normal)
cell.thankButton.addTarget(self, action: "unlike:", forControlEvents: UIControlEvents.TouchUpInside)
}
else {
cell.thankButton.setTitle("Like", forState: UIControlState.Normal)
cell.thankButton.addTarget(self, action: "like:", forControlEvents: UIControlEvents.TouchUpInside)
}
This works fine, as the buttons in each cell display the right title in accordance with the backend. They also have the right function, which code is as follows:
func like(sender:UIButton) {
var id = sender.tag
var postId = self.id[id]
var likeAction = PFObject(className: "Likes")
likeAction["Liker"] = PFUser.currentUser()["displayName"]
likeAction["likedItem"] = postId
likeAction.saveInBackgroundWithBlock { (success:Bool!, error:NSError!) -> Void in
if error == nil {
if success == true {
self.homeTableView.reloadData()
println("liked")
}
}
}
}
func unlike(sender:UIButton) {
var id = sender.tag
var postId = self.id[id]
var query = PFQuery(className: "Likes")
query.whereKey("Liker", equalTo: PFUser.currentUser()["displayName"])
query.whereKey("likedItem", equalTo: postId)
var targetId:String!
query.findObjectsInBackgroundWithBlock { (results:[AnyObject]!, error:NSError!) -> Void in
if error == nil {
targetId = results[0].objectId
var likeObject = PFObject(withoutDataWithClassName: "Likes", objectId: targetId)
likeObject.deleteInBackgroundWithBlock({ (success:Bool!, error:NSError!) -> Void in
if error == nil {
if success == true {
self.homeTableView.reloadData()
println("liked")
}
}
})
}
}
}
However, reloadData never works, and the buttons retain their title and function, even though it should change (as the backend registers the change). I am aware that a recurring reason for reloadData() not to work is that it is not in the right thread, but as far as I can tell, it is here. The "println()" in both functions actually works every time, the backend registers the change every time, but reloadData() never works.
Any idea is greatly appreciated.Thank you!
OK the problem was actually dumb, but I'm posting in case anyone who is a noob like me gets the issue: basically I was calling reloadData without updating anything, since the array of liked posts is refreshed in getLikes(). I put self.getLikes() instead of reloadData() and it works fine.