Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Why do we query the backend where the "recipientIds" key is equal to the current user's objectId. Isn't the recipientIds an array? How could that possibly be equal to a single objectId?
Wouldn't a whereKey:contains: method be more applicable in this case?
This says "query Parse and look for information with the class name "messages."
PFQuery *query = [PFQuery queryWithClassName:@"Messages"];
This says take the query that we just performed and find the ones where the key named "recpientIds is the same as the current user's ID.
[query whereKey: @"recipientIds" equalTo:[[PFUser currentUser] objectId]];
So it's a way of making sure that the user only sees messages that were directed toward them and not anybody else. I believe it looks inside of the array to see if there is any match and doesn't look at the array as a singular object, rather than an object that stores other objects.
I'm not sure what you're really asking. If it works the way it was written, then surely that's what it is doing. Otherwise there would be no way for the constraint to find the recipientIds because the key would just be equal to some array. If you look in the data browser on Parse it is written like this: ["SOU8JBvgFi","b0JFFyAc0l","FajWRNkChD","TKH49WuZru"] which leads me to believe that it is some sort of array of objectIds and the Parse backend system takes care of the job of looping through each value in the array in order to see if anything matches.
Maybe I'm wrong but I can't imagine any other way of Parse doing it?
I'm not a fan of assuming man. One thing about programming is that you can't assume or think anything is happening in the background. You must be 100% sure. I'd believe Parse documentation or a response from Ben Jakuben, but that's about it.
The string is inside of the array. That is what it is finding. I cannot fathom how you do not see what it is doing?
If the string is in the array stored on Parse, it returns the query.
Here it is in the documentation:
Queries on Array Values
For keys with an array type, you can find objects where the key's array value contains 2 by:
// Find objects where the array in arrayKey contains 2.
[query whereKey:@"arrayKey" equalTo:@2];
It literally says it right in the documentation. "array value contains 2." Note the word contains and not "is equal to." Is that official enough?
No need to be hostile. It was just clearly obvious (and unimportant) to me and anyone is capable of reading documentation so I figured you had done that and just didn't see this information that is written as clear as day.
Ben JakubenTreehouse Teacher
Sorry I missed this discussion when it happened. As Eric linked, this query is built from the documentation (and by verifying that it works as expected with different values). I agree that a
whereKey:contains: method makes more sense, but I chalk this up to a quirk in the Parse API. Perhaps a future update will add such a method.
Lol this is the first time you're producing actual evidence rather than "assuming."