2

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.

3 Answers 3

2

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

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

Comments

1

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.

3 Comments

Thanks for the answer, I probably was over complicating, but I found the answer! While your method works, I need to use an NSPredicate, and the reason it couldn't parse the format was because I was using stringWithFormat, which screws up NSPredicates.
For your stated problem, you don't need to use NSPredicate. What other information makes you think you do?
Sorry I didn't specify, I was trying to keep the question as simple as possible. I don't need per-say but it is easier in my scenario as I need to pass it around bunch and command among other things. Thats why I said I wanted to achieve this using an NSPredicate.
0

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.

Comments

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.