0

I've just started using Parse, its been fairly easy to understand until I got to the more advanced aspects such as saving an array of pointers.

I read on a Parse forum that the best way to do this was to save a JSON object to the server that defined an array of types, for example I have an array of users:

[
    {
         "__type":"Pointer",
         "className":"_User",
         "objectId":"YkIlOohXZV"
    },
    {
         "__type":"Pointer",
         "className":"_User",
         "objectId":"MBe4g6t8av"
    }
]

This information gets saved to Parse just fine, however when I come to query the data I never get a Parse error 102.

I have tried:

let myRequestsQuery = PFQuery("Requests")
myRequestsQuery.whereKey("recipients", containsString: PFUser.currentUser().objectId!)

Why am I getting an error 102? Is there a better way to search the array or do I have to query everything back from the server?

4
  • It might be that your (strictly correct) JSON needs to become less correct by omitting the quotes on the keys. But better advice is this: I think the parse forum is incorrect in suggesting forming pointers this way. Don't pass around object ids and form pointers with literal JSON. Pass around objects and assign them directly (as objects) to pointer attributes. Commented Apr 30, 2015 at 20:27
  • Hey Dan, thanks for your suggestion that sounds like a better way of handling things. In future I think I'll just use an sql backend Commented Apr 30, 2015 at 20:51
  • I'd be curious if the JSON formatting is what makes the difference. Can you check? Commented Apr 30, 2015 at 20:57
  • I'm not at my machine right now. Ill let you know tomorrow Commented Apr 30, 2015 at 20:58

1 Answer 1

2

The problem is that you are comparing a pointer to a string, instead of to another parse object.

I work with parse on Android so I'm not sure about the syntax, but you should use a whereKey and equalTo the parse user itself (PFUser.currentUser()) and not a string lookup on it's ID since it's not stored as a string. When arrays are concerned, equalTo searches inside the for a match.

I don't know Swift, but I think it should look something like this in Obj-C:

PFQuery *query = [PFQuery queryWithClassName:@"Requests"];
[query whereKey:@"recipients" equalTo:[PFUser currentUser]];

Again... please excuse any syntax errors...

P.S. just to be sure, here's a working Java code snippet I use in my app:

ParseQuery<ParseObject> query = ParseQuery.getQuery("Requests");
query.whereEqualTo("recipients", ParseUser.getCurrentUser());
Sign up to request clarification or add additional context in comments.

2 Comments

Hey itai, this worked perfectly thanks so much. It's a shame parse don't document these aspects more clearly.
I had no idea parse would compare the inside of the array, I thought it would only perform literal comparisons of the value therefore I assumed array and user wouldn't even be evaluated. Nice to know :) you've helped me clean up a tonne of unnecessary objects being queried and filtered on the front end

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.