iOS - Parse - PFQuery无效

时间:2014-08-02 02:53:57

标签: ios parse-platform key-value pfquery

我正在创建自定义类ChatRequest,但是当我尝试查询它时,它不会返回任何自定义键。

这是我的代码:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    PFQuery *query = [PFQuery queryWithClassName:@"ChatRequest"];
    [query setValue:[PFUser currentUser].username forKey:@"toUser"];
    NSArray *objects = [query findObjects];
    for (NSUInteger i = 0; i < objects.count; i++) {
        PFObject *object = [objects objectAtIndex:i];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Chat Request!" message:object.sendingUser + @"wants to chat with you!" delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
        [alertView show];
    }
}

有人可以帮忙吗?我检查了我的班级是否正确,钥匙在那里,但它仍然无效。

1 个答案:

答案 0 :(得分:2)

您不使用setValue向查询添加约束。您使用whereKey:equalTo:,因此您的代码应为:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    PFQuery *query = [PFQuery queryWithClassName:@"ChatRequest"];
    [query whereKey:@"toUser" equalTo:[PFUser currentUser].username ];
    NSArray *objects = [query findObjects];
    for (NSUInteger i = 0; i < objects.count; i++) {
        PFObject *object = [objects objectAtIndex:i];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Chat Request!" message:object.sendingUser + @"wants to chat with you!" delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
        [alertView show];
    }
}

但是,出于性能原因,像这样同步调用find对象是个坏主意。您应该使用- (void)findObjectsInBackgroundWithBlock:(PFArrayResultBlock)block来允许查询在后台完成。在完成块中,您可以刷新UI。

此外,从设计的角度来看,toUser应该是引用类型列,而不是字符串类型。然后你可以使用

[query whereKey:@"toUser" equalTo:[PFUser currentUser]];