0

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!

Fogmeister
  • 76,236
  • 42
  • 207
  • 306

2 Answers2

1

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

Community
  • 1
  • 1
Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • Ok thanks. That makes a lot of sense. I'll take a look when I'm back at work. – Fogmeister May 08 '14 at 17:08
  • I would actually recommend against using an array for this, not only will you hit size limitations if someone decides to follow thousands of people, you also won't be able to add things like "status" or be able to show how long X has been following Y etc. – Timothy Walters May 08 '14 at 19:38
  • Size limitations should not be a problem, since initWithCapacity on NSArray takes an unsigned int, which is larger on any platform than what is a plausible max number of followings. However, you MIGHT get problems with memory if you get an array with a huge number of objects. But you can easily add status or how long they have been following, as you don't need to add the PFUser object to the array, but rather a following-object with the info you need. – Marius Waldal May 08 '14 at 19:49
  • My main point, though, was not that this was the best solution, but rather that the OP should focus on queries and not relations when designing the data model. – Marius Waldal May 08 '14 at 19:51
0

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.

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49