0

I am creating a custom class ChatRequest, but when I try to query it, it will not return any custom keys.

Here is my code:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    PFQuery *query = [PFQuery queryWithClassName:@"ChatRequest"];
    [query setValue:[PFUser currentUser].username forKey:@"toUser"];
    NSArray *objects = [query findObjects];
    for (NSUInteger i = 0; i < objects.count; i++) {
        PFObject *object = [objects objectAtIndex:i];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Chat Request!" message:object.sendingUser + @"wants to chat with you!" delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
        [alertView show];
    }
}

Can anyone help? I checked that my class was correct and the keys were there, but it still won't work.

1 Answer 1

2

You don't use setValue to add a constraint to a query. You use whereKey:equalTo:, so your code should be:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    PFQuery *query = [PFQuery queryWithClassName:@"ChatRequest"];
    [query whereKey:@"toUser" equalTo:[PFUser currentUser].username ];
    NSArray *objects = [query findObjects];
    for (NSUInteger i = 0; i < objects.count; i++) {
        PFObject *object = [objects objectAtIndex:i];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Chat Request!" message:object.sendingUser + @"wants to chat with you!" delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
        [alertView show];
    }
}

However, for performance reasons it is a bad idea to call the find objects synchronously like this. You should use - (void)findObjectsInBackgroundWithBlock:(PFArrayResultBlock)block to allow the query to complete in the background. In the completion block you can refresh your UI.

Also, from a design point of view your toUser should be a reference type column, not a string type. You could then use

[query whereKey:@"toUser" equalTo:[PFUser currentUser]];
Sign up to request clarification or add additional context in comments.

4 Comments

it still does not work when I try to access the username property: object[@"sendingUser"].username
If sendingUser is a reference type column then you need to add [query includeKey:@"sendingUser"] to have the Parse API retrieve the referenced object
it gives property 'username' not found on object of type (id)
You need to cast the object - PFUser *sender=(PFUser *)object[@"sendingUser"]; NSString *senderName=sender.username

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.