Advertisement
Guest User

f

a guest
Nov 24th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. I am trying to check if the current user has liked a post uploaded in my application. I am using UICollectionView. The code looks like this:
  2.  
  3. var query2 = PFQuery(className: "currentUploads")
  4. query2.whereKey("objectId", equalTo: post.objID)
  5. query2.whereKey("likedBy", equalTo: self.thisUser!)
  6. query2.getFirstObjectInBackgroundWithBlock {
  7. (object: PFObject?, error: NSError?) -> Void in
  8. if error != nil || object == nil {
  9. println("Like does not exist.")
  10. cell.likeButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
  11. }
  12. else{
  13. println("Like does exist.")
  14. cell.likeButton.setTitleColor(UIColor.redColor(), forState: .Normal)
  15. }
  16. }
  17.  
  18. I have been using that code in cellForItemAtIndexPath, and I heard that I should never query in cellForItemAtIndexPath. The "post.objID" is the object id from parse for the current object row. The `"likedBy"` is a array in parse that contains usernames, and "self.thisUser" is the username for the current user.
  19.  
  20. Like button action:
  21.  
  22. @IBAction func likeButtonAction(sender: AnyObject) {
  23. let buttonPosition = sender.convertPoint(CGPointZero, toView: self.collectionView)
  24. let indexPath = self.collectionView.indexPathForItemAtPoint(buttonPosition)
  25. ////
  26. //println(indexPath?.item)
  27. ////
  28. let post = self.arrayOfDetails[indexPath!.item]
  29. println(post.likedBy)
  30.  
  31. let cell = collectionView.cellForItemAtIndexPath(indexPath!) as! CollectionViewCell
  32.  
  33. //cell.likeButton.enabled = false
  34.  
  35. if (post.likedBy.containsObject(thisUser!)){
  36. let queryDislike = PFObject(withoutDataWithClassName: "currentUploads", objectId: post.objID)
  37.  
  38. queryDislike.removeObject(self.thisUser!, forKey: "likedBy")
  39. println("STEP 1: Current user removed from likedBy column")
  40.  
  41. //Save
  42. queryDislike.saveInBackgroundWithBlock {
  43. (success: Bool, error: NSError?) -> Void in
  44. if (success) {
  45. // The object has been incremented
  46. println("STEP 3: Successfully saved")
  47. self.queryFromParse()
  48.  
  49. } else {
  50. // There was a problem, check error.description
  51. //println(error!.description)
  52. }
  53. }
  54. }
  55. else{
  56. let queryLike = PFObject(withoutDataWithClassName: "currentUploads", objectId: post.objID)
  57.  
  58. queryLike.addObject(self.thisUser!, forKey: "likedBy")
  59. println("STEP 1: Current user added to likedBy column")
  60.  
  61. // Save
  62. queryLike.saveInBackgroundWithBlock {
  63. (success: Bool, error: NSError?) -> Void in
  64. if (success) {
  65. // The object has been incremented
  66. println("STEP 3: Successfully saved")
  67. self.queryFromParse()
  68.  
  69. } else {
  70. // There was a problem, check error.description
  71. //println(error!.description)
  72. }
  73. }
  74. }
  75. }
  76.  
  77. And suggestions on how I can do this? If the user already has liked a photo, the button will be red, if the user has not liked the photo, the button will be blue(color is for testing only).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement