3

Sorry if this is an extremely simple and dumb question. I'm still really new to Parse.

Suppose I have a Schedule object, and a Schedule object can have a few ScheduleItem objects (not many). I decided to model this using arrays for relations, such as:

PFObject *first = ...
PFObject *second = ...
PFObject *third = ...

PFObject *schedule = [PFObject objectWithClassName:@Schedule"];
schedule[@"scheduleItems"] = @[first, second, third];

Suppose I want to later query for these ScheduleItems which belong to this schedule AND use it in a PFQueryTableViewController. I can't just query for this schedule and access its items through the array because PFQueryTableViewController uses the results of the PFQuery as the data source for the table view. And I'm not sure how to query for "the ScheduleItem objects that live in the array 'scheduleItems' on a particular Schedule object". I see the query method whereKey:containedIn:, but it doesn't quite seem to match, because I don't have a reference to this array before I do the query. It does seem like I need to do some sort of nested/compound query because for part of this query, I need to query for the Schedule object, but I'm not sure exactly how this works.

EDIT: To clarify this question. Suppose the Schedule object has a name "Today". I know I can construct a query for that Schedule object and tell it to include the "scheduleItems" field:

PFQuery *query = [PFQuery queryWithClassName:@"Schedule"];
[query whereKey:@"name" equalTo:@"Today"];    // or query by its objectId
[query includeKey:@"scheduleItems"];

I know that if I execute this query with something like findObjectsInBackgroundWithBlock:, the direct query result is an array of one object, the Schedule object, with all of the ScheduleItems available through the scheduleItems field.

My problem is, I don't want to execute this query myself - I want to use those ScheduleItems as the cells in a PFQueryTableViewController, which assumes a one-to-one mapping between cells and the direct query result objects. If I use the above query with the PFQueryTableViewController, the direct query result is an array of one object, meaning the table only has one object. In short, I don't know how to tell PFQueryTableViewController to do a query, but then use its related array of objects to populate the table.

UberJason
  • 3,063
  • 2
  • 25
  • 50
  • For you tell your query to include that column? `[query includeKey:@"scheduleItems"];` See doc for using arrays as relations here: https://www.parse.com/docs/ios/guide#relations-using-an-array – kRiZ Jun 30 '15 at 04:16

1 Answers1

0

Your choice of using an array instead of a relationship is perfect for your use case. Because of that you can specify

[query includeKey:@"scheduleItems"];

after creating your query to specify that it should include, inline and requiring no second network request, the details for the objects in the array.

Remember that you should keep the number of objects in the array small to avoid performance and potentially size issues.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Sorry if I wasn't clear in my initial question. I knew I could use includeKey: to get the scheduleItems array in the query, but I don't know how to make this work with PFQueryTableViewController. I want each of these ScheduleItems to be a row in the table, but if I execute a query on Schedule and use includeKey, the query itself would only return one result, and the table would only have one row. Of course I could just do this query and set up the table manually, but I was hoping to take advantage of PFQueryTableViewController to handle most of it. – UberJason Jun 30 '15 at 12:43
  • So you need to configure that yourself, for instance by converting your subclass so it create one section per object in the response and sets the row count for each section to `(1 + messages.count)`. I think the sample AnyPic app from Parse does basically the same thing as a customisation of `PFQueryTableViewController` – Wain Jun 30 '15 at 12:46
  • That configuration is what I've been stumbling so hard on. :) Far as I can tell, PFQueryTableViewController assumes a one-to-one mapping between your direct query result objects and your table cells. If I query on Schedule, the query result has only one object, even though that one object has several related ScheduleItems. So even creating 'one section per object in the response' would still only create one. I looked at AnyPic, they don't have any array-type relations like I have, so that doesn't quite seem to apply. – UberJason Jun 30 '15 at 13:17
  • It does apply. You need to override the section and row delegate methods and configure the cells appropriately for each row (where each row is either the schedule or one of its messages) – Wain Jun 30 '15 at 14:29
  • Okay, I guess I need to still do some manual table view management then. That's totally fine, I was just wondering if there was a way for Parse to "do it for me". Thanks! – UberJason Jun 30 '15 at 16:40