使用Parse.com指针

时间:2014-09-21 09:24:51

标签: ios objective-c pointers parse-platform

我目前正在开发我的第一款应用。该应用程序是基于Parse.com的基本信使应用程序。

我想创建一个PFQueryTableViewController,它会显示最近与其他用户的聊天内容。

Photo其他用户,Name其他用户和timestamp(类似于Facebook messanger"最近"标签)。

聊天数据保存在名为Room的Parse类中,其中包含以下列:

  1. 的objectid(字符串)
  2. ROOMNAME(字符串)
  3. User_1(指向_User的指针)
  4. User_2(指向_User的指针)......
  5. 我可以使用字符串值(例如房间名)轻松填充表格视图,但我希望将用户@"full name"作为每个单元格的标签。

    这是我的代码(我得到一个空的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;
    }
    

2 个答案:

答案 0 :(得分:0)

当指针对象最初可用时,它只是一个存根,它实际上并不包含任何数据。您需要在其上调用fetchAllIfNeededInBackground:block:(为了保持高效),以填充数据。

查看子视图表视图控制器并覆盖- (void)objectsDidLoad:(NSError *)error以触发对新对象的提取。

请注意,您可能只想更改Room类以缓存用户名(但如果您这样做,则在用户名更改时需要一些云代码来更新缓存)。

答案 1 :(得分:0)

好的,我解决了,希望这有助于其他人。 这是更新的工作代码:

- (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
相关问题