Hi.
How do I check if a string is already contained in a table of a class? I’m making an app where users get to rsvp and I’d like it to work as, if they already rsvped / their user id is already in the string table for “person,” the button’s action removes them if clicked a second time.
In parse, it was something like this:
BOOL likedAlready = true;
PFObject *tempObject = [postArray objectAtIndex:senderButton.tag];
NSLog(@"%@", tempObject.objectId);
[[PFUser currentUser]addUniqueObject:tempObject.objectId forKey:@"liked"];
[[PFUser currentUser] saveInBackground];
if (likedAlready)
{
PFQuery* query = [PFQuery queryWithClassName:@"Like"];
[query whereKey:@"photo" equalTo:tempObject];
[query whereKey:@"forUser" equalTo:[PFUser currentUser].objectId];
NSArray* objects = [query findObjects];
if(objects.count > 0)
{
PFObject* like = objects.firstObject;
[like deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
PFQuery *query = [PFQuery queryWithClassName:@"Like"];
[query whereKey:@"photo" equalTo:tempObject];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error == nil) {
NSLog(@"Number: %lu", (unsigned long)objects.count);
cell.likeLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)objects.count];
[cell.liked setBackgroundImage:[UIImage imageNamed:@"liked.png"] forState:UIControlStateNormal];
}
}];
}];
}
else{
PFObject* like = [PFObject objectWithClassName:@"Like"];
[like setObject:[PFUser currentUser][@"username"] forKey:@"username"];
[like setObject:[PFUser currentUser].objectId forKey:@"forUser"];
[like setObject:tempObject forKey:@"photo"];
[like saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//1. Check isSuccess
if (succeeded) {
PFQuery *query = [PFQuery queryWithClassName:@"Like"];
[query whereKey:@"photo" equalTo:tempObject];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error == nil) {
NSLog(@"Number: %lu", (unsigned long)objects.count);
cell.likeLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)objects.count];
[cell.liked setBackgroundImage:[UIImage imageNamed:@"hearted@2x.png"] forState:UIControlStateNormal];
}
}];
}
}];
}
}
In this case, “Like” is “RSVP”! But that’s basically what I have in mind, like / unlike