Josh Kahane
Josh Kahane

Reputation: 17160

Parse PFQuery Predicate Pointer Object

I am performing a simple PFQuery to fetch a bunch of Box objects (class name).

My Box has a pointer to a Toy object called toy.

I let my user select a bunch a toys, then the search only display the Box objects with those Toy objects.

So I end up with an NSArray of PFObjects of type Toy. I have an array of objectId strings for the objects, and I just create another array like this:

PFObject *obj = [PFObject objectWithoutDataWithClassName:@"Toy" objectId:objectId];

Now I can query the object, I would have thought. I have tried doing a query of Box objects with an NSPredicate which looks like this:

[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"toy = %@", toyObject]];

My app crashes and tells me it is unable to parse that. So before adding the predicate I take the objectId instead and try doing that:

[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"toy.objectId = %@", toyObject.objectId]];

However, it doesn't like that format either. So how can I create an NSPredicate that lets me only fetch objects with a specific pointer result like explained.

Upvotes: 2

Views: 867

Answers (3)

poopit
poopit

Reputation: 169

In other words just use

 PFUser *user = [PFUser currentUser];
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fromUser = %@", user];

where in this example, "user" is a pointer stored in the table and we're searching for it under the "fromUser" column

Upvotes: 2

Josh Kahane
Josh Kahane

Reputation: 17160

Turns out it was pretty simple.

Don't use [NSString stringWithFormat:(NSString *)] if you are creating an NSPredicate format string. NSPredicate really doesn't like it and can often fail to parse your format.

Upvotes: 0

mbm29414
mbm29414

Reputation: 11598

I think you're making this too hard on yourself. If I'm reading your question correctly, there is a much simpler way to do this, and you don't have to use NSPredicate at all:

NSMutableArray *toys = [NSMutableArray array];
// Figure out some way (during selection) to get the "toy" objects into the array
PFQuery *query = [PFQuery queryWithClassName:@"Box"];
[query whereKey:@"toy" containedIn:toys];
[query findObjects... // You should know the rest here

And that's it! Nice and easy, should find all Box instances that have a toy that is contained in the toys array.

Upvotes: 1

Related Questions