Domanda

Sto creando un ChatRequest di classe personalizzato, ma quando provo a interrogarlo, non restituirà alcun tasto personalizzato.

Ecco il mio codice:

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

Qualcuno può aiutare?Ho controllato che la mia classe fosse corretta e le chiavi erano lì, ma ancora non funzionerà.

È stato utile?

Soluzione

Non si utilizza setValue per aggiungere un vincolo a una query.Si utilizza whereKey:equalTo:, quindi il tuo codice dovrebbe essere:

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

Tuttavia, per motivi performance è una cattiva idea chiamare gli oggetti trovati in modo sincrono come questo.È necessario utilizzare - (void)findObjectsInBackgroundWithBlock:(PFArrayResultBlock)block per consentire alla query di completare in background.Nel blocco di completamento puoi aggiornare il tuo UI.

Inoltre, da un punto di progettazione del toUser dovrebbe essere una colonna Tipo di riferimento, non un tipo di stringa.Potresti quindi usare

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top