查询Parse指针时 - 返回nil

时间:2014-02-16 13:05:38

标签: ios parsing pointers

我在Parse中有一个带有列的表

  

ObjectId |邀请者(指针)|活跃(Bool)| createdAt(日期)

我正在尝试使用Inviter信息填充表。所以我试图在查询过程中拉指针。

以下是提取此数据的方法

- (void) getInvitationsForLoggedInUser {
    NSLog(@"entered getInvitationsForLoggedInUser");
    PFQuery *postQuery = [PFQuery queryWithClassName:@"Connection"];
    [postQuery whereKey:@"Active" equalTo:[NSNumber numberWithBool:NO]];
    [postQuery includeKey:@"Inviter"];

    [postQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            [self.tableData addObjectsFromArray:objects];
            NSLog(@"InvitationViewController - Loaded %lu connection objects from Parse for logged in user", (unsigned long)[objects count]);
            [self.invitationTableView reloadData];
        } else {
            NSLog(@"InvitationViewController - Could not find any connection objects from Parse");
        }
    }];
}

这是我从tableData数组调用数据的地方。

- (InvitationCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //TOFIX: In the view, I am seeing the entry but the fields are null. Need to check that the cell fields are getting populated.

    static NSString *cellIdentifier = @"Identifier";
    InvitationCell *cell = (InvitationCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    // Create a new PFObject Object
    PFObject *connection = nil;

    connection = [self.tableData objectAtIndex:[indexPath row]];

    PFObject *inviterCodeName = [connection objectForKey:@"Inviter"];
    if (inviterCodeName == nil) {
        NSLog(@"Error: Could not retrieve 'Inviter' object from connection object with id %@", [connection objectId]);
    }

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:enUSPOSIXLocale];
    [dateFormatter setDateFormat:@"EEE, MMM, h:mm a"];

    cell.dateLabel.text = [dateFormatter stringFromDate:[connection updatedAt]];

    cell.codeName.text = [NSString stringWithFormat:@"Inviter Code Name: %@", inviterCodeName[@"codeName"]];
    if (inviterCodeName[@"description"] == nil) {
        cell.codeNameDescription.text = @"";
    } else {
        cell.codeNameDescription.text = [NSString stringWithFormat:@"%@", inviterCodeName[@"description"]];
    }

    return cell;
}

为了完整, 这些都在这个生命周期方法中被调用

- (void) viewWillAppear:(BOOL)animated {
    [self getInvitationsForLoggedInUser];
    [super viewWillAppear:animated];
    [self.invitationTableView reloadData];
}

现在,在cellForRowAtIndexPath中,我成功检索了Connection对象。当我尝试获取“inviterCodeName”对象时,它始终为零。

在我测试时,表中只有一行符合这些要求,并且已成功填充。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这似乎是一个异步问题。该表在后台解析线程返回数据之前加载。我将呼叫切换为同步。

我不是百分之百确定这是'正确'的方法,但确实解决了这个问题。