-1

I'm currently developing my first App. The App is a Basic messenger App based on Parse.com.

I want to create a PFQueryTableViewController where it will show the recent chats with other users.

Photo of other user, Name of other user and timestamp (similar to Facebook messanger "recent" tab).

the chats data is saved in a Parse Class named Room with the following columns:

  1. objectid(string)
  2. roomname(string)
  3. User_1(pointer to _User)
  4. User_2(pointer to _User)...

I can fill the table view easily with the string values (e.g the roomname) but I would like to get the users @"full name" as the Label of each cell.

this is my code (I get an empty TableView):

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:@"Room"];
    [query includeKey:@"User_2"];

    if (self.objects.count == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    [query orderByDescending:@"createdAt"];

    return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object
{
    static NSString *cellIdentifier = @"Cell";

    PFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = object[@"fullname"];

    return cell;
}
1

2 Answers 2

0

When the pointer object is initially available it's just a stub, it doesn't actually contain any data. You need to call fetchAllIfNeededInBackground:block: on it (them, to keep things efficient) to populate the data.

Look at subclassing the table view controller and overriding - (void)objectsDidLoad:(NSError *)error to trigger the fetch on the new objects.

Note that you might just want to change your Room class to cache the user names (though if you do that you will need some cloud code to update the caches if the user name changes).

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

5 Comments

Thanks for the quick answer. but im not sure i understand. Could you maybe provide an example (its my first week learning how to code), Thank you.
What specifically don't you understand? When using pointers you don't immediately have the info you need, you need to make an additional request to get it. The table controller will only get your Room objects data, you need to additionally request the data for the users that they point to.
That i understand, but i'm not sure how to request the additional information.I tried adding this under the includeKey query: [query findObjectsInBackgroundWithBlock:^(NSArray *postObjects, NSError *error) { if (self.objects.count == 0) { query.cachePolicy = kPFCachePolicyCacheThenNetwork; } [query orderByDescending:@"createdAt"]; return query; } ]}; But got an Error: incompatible block pointer types....
You need to get the objects from the table controller and use them in a call to fetchAllIfNeededInBackground:block:. This is separate to the query that you give to the controller and after that query has returned data...
Seems there are a few subjects i need to learn before i can continue. Thanks for the Help. I will be back with my solution.
0

Ok, I solved it, hope this helps others. This is the updated working code:

- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:@"Room"];
[query includeKey:@"User_2"];

// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if (self.objects.count == 0) {
    query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}

[query orderByDescending:@"createdAt"];

return query;
}

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

static NSString *cellIdentifier = @"Cell";

PFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
    cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:cellIdentifier];
}



PFObject *user = object[@"User_2"];

[user fetchIfNeededInBackgroundWithBlock:^(PFObject *user, NSError *error) {
    //NSLog(@"%@", user);

    cell.textLabel.text = user[@"fullname"];
}];

return cell;
}

@end

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.