I'm building an app which has the following classes
objectId
username
ObjectId
Recipe Name
Recipe File
UploadedByUser(Pointer to class Users)
LikedByUser(Pointer to class Users)
LikedRecipe(Pointer to class Recipes)
So if a user has already liked one recipe, the image of the button inside the cell should have a specific BackgroundImage, and if is not should have a different one. What i've tried is this
let likesClassy = PFObject(className: LIKES_CLASS_NAME)
let currentUser = PFUser.currentUser()
if likesClassy[LIKES_LIKED_BY] === currentUser! {
let imgLiked = UIImage(named: "likedButt") as UIImage!
cell.likeOutlet.setBackgroundImage(imgLiked, forState: UIControlState.Normal)
} else {
let imgUnLiked = UIImage(named: "likeButt") as UIImage!
cell.likeOutlet.setBackgroundImage(imgUnLiked, forState: UIControlState.Normal)
}
And i have this code inside the collectionView where i load also how many likes the recipe has etc. I attach you the image from the parse.com class below
So in the if
statement i'm trying to say that if the current user is equal with the userpointer of the Likes class, then the background image of the button should be likedButt, else should be likeButt.
But this doesnt work and i cant figure out why. Any idea?
Update. I attach you the full likes class
You didn't fetch objects in Parse server. You should fetch the objects with some predicate options. This is the function you should use. It looks the your currentUser and your LIKES_LIKED_BY row in your Parse. If your LIKES_LIKED_BY row has a currentUser it fetches users.
func getLikedUsers() {
let user = PFUser.currentUser()
let query = PFQuery(className: "LIKES_CLASS_NAME")
query.whereKey("LIKES_LIKED_BY", equalTo: user!)
query.findObjectsInBackgroundWithBlock() {
(let fetchedObjects, error) in
if error == nil {
// Query succeed.
if fetchedObjects?.isEmpty == false {
//If your currentUser already liked.
if let fetchedUsers = fetchedObjects as? [PFUser] {
for fetchedUser in fetchedUsers {
print(fetchedUser.username)
// You got the users Do someting with your liked users.
}
}
} else {
// Your user didn't liked
}
} else {
// Parse Query fail. Do someting with error.
print(error?.localizedDescription)
}
}
}