0

カスタム クラスを作成してい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];
    }
}

誰でも助けることができますか?クラスが正しく、キーがそこにあることを確認しましたが、それでも機能しません。

4

1 に答える 1

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];
    }
}

ただし、パフォーマンス上の理由から、このように検索オブジェクトを同期的に呼び出すことはお勧めできません。- (void)findObjectsInBackgroundWithBlock:(PFArrayResultBlock)blockクエリをバックグラウンドで完了できるようにするために使用する必要があります。完了ブロックでは、UI を更新できます。

また、設計の観点からtoUserは、文字列型ではなく、参照型の列にする必要があります。その後、使用できます

[query whereKey:@"toUser" equalTo:[PFUser currentUser]];
于 2014-08-02T03:13:33.763 に答える