Find Objects?

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 :slight_smile:

Basically, it adds a new one every time the user clicks the button. I want it to add it just once and if clicked again, remove it completely. I’m thinking it has something to do with NSArray, it’s count, and If statement? I’m not sure how to accomplish it though.

You should investigate this doc. Also you could use this sample project.

Hey Vya, thanks for replying. It’s a bit tough navigating through the docs! Will I have to do a query? How does the whereClause come into play?

Hey, good news Vya! I think I got it working but I’m an issue with reloading the app when the button is clicked.

Step 1:

- (void)getAllEntitysAsync
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    QueryOptions *query = [QueryOptions query];
    BackendlessDataQuery *dataQuery = [BackendlessDataQuery query:nil where:nil query:query];
    dataQuery.whereClause = [NSString stringWithFormat:@"person = \'%@\'", currentUser.objectId];
    dataQuery.whereClause = [NSString stringWithFormat:@"eventday = \'%@\'", @"May 1, 2016"];
    Responder *responder = [Responder responder:self selResponseHandler:@selector(responseHandler:) selErrorHandler:@selector(errorHandler:)];
    [backendless.persistenceService find:[Rsvp class] dataQuery:dataQuery responder:responder];
}

Step 2:



- (void)responseHandler:(id)response
{
    
    NSLog(@"responseHandler: class = %@", [response class]);
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    [_data addObjectsFromArray:[(BackendlessCollection *)response data]];
    
    if([_data count]==0)
    {
        [self.rsvpButton addTarget:self action:@selector(addPerson:) forControlEvents:UIControlEventTouchUpInside];}
    
    else {
        
        [self.rsvpButton addTarget:self action:@selector(removePerson:) forControlEvents:UIControlEventTouchUpInside];
   
    }


}

Action 1:

-(void)addPerson:(id)sender {
    NSLog(@"Count %d", _data.count);
    Responder *responder = [Responder responder:self
                             selResponseHandler:@selector(responseHand:)
                                selErrorHandler:@selector(errorHandler:)];
    Rsvp *task = [Rsvp new];
    [task setPerson:currentUser.objectId];
    [task setEventday:@"May 1, 2016"];
    [task setStatus:[NSNumber numberWithBool:NO]];
    id<IDataStore> dataStore = [backendless.persistenceService of:[Rsvp class]];
    [dataStore save:task responder:responder];
    NSLog(@"Nothing to see here");




}

Action 2:

-(void)removePerson:(id)sender {
    
    UIButton *senderButton = (UIButton *)sender;
    NSLog(@"current Row=%d",senderButton.tag);
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [backendless.persistenceService remove:[Rsvp class] sid:[(Rsvp *)[_data objectAtIndex:senderButton.tag] valueForKey:@"objectId"] response:^(NSNumber *response) {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        [_data removeObjectAtIndex:senderButton.tag];
    } error:^(Fault *fault) {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        NSLog(@"StartViewController -> removeObjectWithIndex: FAULT = %@ <%@>", fault.message, fault.detail);
        [self showAlert:fault.message];
    }];


    NSLog(@"Oh, hi there");
}

How do I reload the class after the user has clicked the button? Even with [self getAllEntitysAsync] in the actions, nothing happens for some reason.

I added an action to unlike to the response of the like and it works! Thank you :slight_smile: