Fogmeister
Fogmeister

Reputation: 77641

Parse query depending on sub query

So I have an object in my Parse DB called Follow like this...

Follow
---------------
PFUser follower
PFUser followee

i.e. this is a join table for a many-many "following" structure. A user can follow many users and a user can be followed by many users.

Anyway, I'm trying to create a query that just returns an array of PFUser objects that are users the the currentUser follows.

I've started like this...

PFQuery *followQuery = [Follow query];
[followQuery whereKey:@"follower" equalTo:[PFUser currentUser]];

This will return an array of follow objects but I don't want these. I want the array of PFUser objects and I'm stuck how to get there from here.

I feel like this should be a lot easier than I'm making it. lol!

Upvotes: 0

Views: 1471

Answers (2)

Timothy Walters
Timothy Walters

Reputation: 16874

You existing structure works, the issue is that you have a return result that is an array of Follow objects, and you want an array of followee.

First off you probably want to tell the query to includeKey for followee so that you have the full user instead of just a pointer with an ID.

Secondly you want to map the results, which you can do just like you would any other array of objects you want to read a property out of. Just use a loop.

Upvotes: 0

Marius Waldal
Marius Waldal

Reputation: 9942

It is a lot easier. And harder, depending on your mindset. Your apparent mindset: SQL :-)

When working with Parse (or other NoSQL datastores), especially from a mobile device, focus on your queries first, and create a model that will keep the commonly used queries a simple and as few as possible, and also calculation on the device to a minimum.

For your use case, create a class to hold all followers and all followings for every user in an array.

I have explained this in an earlier answer: https://stackoverflow.com/a/22449103/1485715

Also, as linked in that answer, the Twissandra project for Java is a good primer (for all programmers; not just Java-programmers) on how to model for NoSQL: https://github.com/twissandra/twissandra

Upvotes: 1

Related Questions