mardi 3 mars 2015

Random item from Parse database

I want a random object from my database, but it doesn't work.


This is how my database looks like:



PFObject *master = [[PFQuery queryWithClassName:@"items"] getFirstObject];
int maxIndex = [master objectForKey:@"index"];

NSLog(@"MaxIndex = %d", maxIndex);

// Randomly pick an index in the range maxIndex.
int randomIndex = arc4random() % maxIndex;

// Get the card with that particular index.
PFQuery *cardQuery = [PFQuery queryWithClassName:@"items"];
[cardQuery whereKey:@"index" equalTo:[NSNumber numberWithInt:randomIndex]];
PFObject *card = [cardQuery getFirstObject];

NSLog(@"The next item is picked: %@", card);
label.text = card;



What's the problem? The problem is that it outputs maxIndex as 19, even while there are only 2 items in the database. And no results matched the query.





Aucun commentaire:

Enregistrer un commentaire