5

In my "Message" table in Parse I have a field called conversation, which is a pointer to a Conversation (another table in my database).

To query for a Message, can I do:

    PFQuery *messageQuery = [PFQuery queryWithClassName:@"Message"];
    [messageQuery whereKey:@"conversation" equalTo:_conversation.objectid];
    [messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

          ...

    }];

or do I have to get the actual PFObject *myConversation and use that...

    PFQuery *messageQuery = [PFQuery queryWithClassName:@"Message"];
    [messageQuery whereKey:@"conversation" equalTo:myConversation];
    [messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

          ...

    }];

It seems that #1 doesn't work, but #2 does...my question is how can I get #1 to work (i.e. use a PFObject's id to query when I have a pointer field)

1 Answer 1

5

.objectId is just a string, if your "conversation" key contains a pointer to myConversation, then you must include a PFObject in the equal to.

If you only have the objectId, you can search pointers without data using:

PFObject * myConversation = [PFObject objectWithoutDataWithClassName:@"Conversation" objectId:_conversation.objectid];

// continue here

[messageQuery whereKey:@"conversation" equalTo:myConversation];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

      ...

}];
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your response. Is this as efficient as if I had the object? or is this still doing a string comparison in the backend?
i.e. if I had the object, it would do a pointer comparison in the backend because the field is of pointers. In the method you describe above, will it be just as though I was storing objectId's instead of pointers since this will do a string comparison? Or does this do some other fancy thing...
Pointers only store the objectId and classname anyways, so I'd say it's probably about the same. Using objectwithoutdata is probably faster only because you wouldn't have to download the object from the server first.
right, but if I already have a copy of the object locally, would it be better to just use the object itself instead of the objectId?
It really depends how you use it, I don't think you'll get a lot of downtime on parse's servers and the upload of a few extra characters for a pointer certainly isn't too much data. Storing a pointer has the benefit of being able to fetch that object in a query. For your purposes, I'd say whichever you feel like is fine. I'm pretty sure in the parse docs they even mention you can do either. There was no mention of performance considerations.

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.