Steve
Steve

Reputation: 959

Retrieve related data with parse and ios sdk

I'm just trying to retrieve related data into my app with Parse but I'm getting some problems. I have 2 tables, Travel and City, Travel has a related field called origin-city. I'm using a pointer in Data Browser to relate both of them.

So in queryForTable method i'm using

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query includeKey:@"origin-city"];
    return query;

}

And I'm always getting the pointer Id but not the City name that's what I really need. This is the proper method for doing this ? how could I retrieve the name of the city ?

Edit

When I print the origin city I'm getting City:M0PwR0OiLj:(null) where M0PwR0OiLj is the objectId for City, here I need the name

Thanks a lot

Upvotes: 1

Views: 702

Answers (3)

Marius Waldal
Marius Waldal

Reputation: 9932

I assume you are using a PFQueryTableViewController, as the method queryForTable belongs to that. The error stating that a query is already in progress is because the query is fired behind the scenes by the PFQTVC, so the answer asking to do a findObjectsInBackgroundWithBlock is not possible in your case.

With this special table view controller, the cellForRowAtIndexPath is also passed a PFObject, which is the object to matching the row.

To get the city name, which is from related object, you use this code in cellForRowAtIndexPath:

PFObject *city = object[@"origin-city"];
[cell.textLabel setText:city[@"name"]; // The name column from the City class

Upvotes: 2

Luca Iaco
Luca Iaco

Reputation: 3457

Check if:

  1. self.parseClassName contains the className string "Travel"

  2. from your Parse web dashboard, ensure the "origin-city" column in "Travel" class is really a pointer of type "City"

  3. pointer refer to a City row that still exist

Remember that each empty field (from web dashboard you can see the fields/columns with the "undefined" placeholder into them) is not returned in the result query. So it means that if you have an empty (so, undefined) column "name" in the "City" className, you won't be able to read it. Anyway the selection should be like this:

[query findObjectsInBackgroundWithBlock:^(NSArray * travels, NSError *error) {

    if (error) 
        return;

    for (PFObject *travelX in travels) {

        PFObject *city = travelX[@"origin-city"]; // or [travelX objectForKey:@"origin-city"] if you prefer

        if (city){

            NSString* objectId = city.objectId;
            NSDate* createdAt = city.createdAt;
            NSString* cityName = city[@"name"];
            NSLog("The city name is %@", ( cityName ? cityName : @"<NOT DEFINED>" ) );

        }else
            NSLog(@"%@",@"There is no city for this travel");    

    }
}];

Hope it helps

Upvotes: 0

zoruc
zoruc

Reputation: 819

I believe when you use the - (PFQuery *)queryForTable it will return the results it pulls to cell for row at index path so you could try something like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

 [cell.textLabel setText:[object objectForKey:@"origin-city"]];

return cell;

}

Upvotes: 1

Related Questions