解析查询不在哪里调用

时间:2014-08-02 19:28:15

标签: objective-c sdk parse-platform

出于某种原因,我的解析查询中的where键似乎不起作用。

    NSString *uploaderId = [object[@"Uploader"] valueForKey:@"objectId"];

    PFQuery *findLikes = [PFQuery queryWithClassName:@"Activity"];
    [findLikes whereKey:@"type" equalTo:@"Like"];
    [findLikes whereKey:@"from" equalTo:uploaderId];
    [findLikes findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        NSLog(@"Amount of likes for cell: %lu", (unsigned long)objects.count);
    }];

即使活动表中有项目,计数也为0。

澄清一下,from是一个特定用户的指针。

另外,我想以不同的标签显示照片的前四个颜色。我该怎么做?

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

    static NSString *CellIdentifier = @"Cell";

    FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[FeedTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle     reuseIdentifier:CellIdentifier];
    }

    PFQuery * findLikes = [PFQuery queryWithClassName:@"Activity"];
    [findLikes includeKey:@"from"];
    [findLikes includeKey:@"recapId"];
    [findLikes whereKey:@"from" equalTo:object[@"Uploader"]];
    [findLikes whereKey:@"recapId" equalTo:object];
    [findLikes whereKey:@"type" equalTo:@"Like"];
    [findLikes findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        NSLog(@"Amount of likes for cell: %lu", (unsigned long)objects.count);
        for (PFObject *likers in objects) {
            cell.liker.text = [[likers objectForKey:@"from"] valueForKey:@"name"];
        }
    }];

1 个答案:

答案 0 :(得分:1)

这是一个关系查询,所以你可以使用对象本身(看起来像是对象[@“Uploader”])...

PFQuery *findLikes = [PFQuery queryWithClassName:@"Activity"];
[findLikes whereKey:@"from" equalTo:object[@"Uploader"]];
// plus your other criterion

或者,如果你更愿意使用id ...

[findLikes whereKey:@"from"
            equalTo:[PFObject objectWithoutDataWithClassName:@"User" objectId:uploaderId]];

但我认为第一种方式更简单。

编辑 - 关于表格查看问题。 cellForRowAtIndexPath不是异步查询的好地方。每当一个细胞进入视野时,它就会一次又一次地运行。正确的模式是为表视图创建一个模型,该模型是一个Activity对象数组。

@property(strong,nonatomic) NSArray *activityArray;

早期的某个地方,比如viewWillAppear:,执行一次查询并使用结果初始化数组,然后告诉您的表格视图:

// viewWilAppear
[findLikes findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    // if no error ...
    self.activityArray = objects;  
    [self.tableView reloadData];
}];

然后你的数据源:

// in cellForRowAtIndexPath
PFObject *liker = self.activityArray[indexPath.row];
cell.liker.text = [[liker objectForKey:@"from"] valueForKey:@"name"];