jeudi 26 mars 2015

How can I query/retrieve a random object from a parse class using swift?

I've looked all over and have found no joy. I was wondering if someone could help me figure out how to retrieve a random object from a class on parse.com using swift in an iOS app. By no means am I asking someone to write my code, because then what would I learn, but I was wondering if someone could maybe provide a generic example that I could adapt to my project and future projects.


Let's say the class is called ParseClass, and I will need to populate three variables with data from the object in parse., A, B, C -- two with strings, one as an array of strings. Let's say there are ... idk ... 50 objects in the parse class, and I need to retrieve them one at a time randomly.


Logically, I get it ... I need to do a count of the objects in the parseclass, then get a random number from that count, and then use that number to retrieve the object somehow (either directly from parse using a skip random query limit 1, or maybe by getting all the objects into an array (whichever is the best/most efficient code). I just don't know how to format/write the code in swift. Any one think they could help me (and many others apparently) with some generic code I could adapt to my specific project??


Here is some generic code ... I can start it -- I got a basic idea of how it should be, I just don't know swift well enough to complete the block.



var A : String!
var B : [String]!
var C : String!

var query : PFQuery = PFQuery(className: "ParseClass")
query.findObjectsInBackgroundWithBlock {
(ObjectHolder : [AnyObject]!, error : NSError!) -> Void in


//now what? I've seen this in other questions here, but I don't know how to incorporate it.



let randomSkip = arc4random_uniform(count)

query.skip = randomSkip, and query.limit = 1.


Any help on this would be greatly appreciated.


Oh -- just saw this in another thread ... it's basically doing what I need, but in objective C and it looks like with only 2 variables... could someone help me rewrite in swift? Sorry to be so loquacious ... the burden of a novice. I promise as I grow more adept, I will help other novices most sympathetically. :-)



- (void)randomQuestion:(void (^)(NSString *question, NSArray *answers))completion {
PFQuery *countQuery = [PFQuery queryWithClassName:@"ParseClass"];
[countQuery countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
NSInteger randomSkip = arc4random_uniform(count);
PFQuery *query = [PFQuery queryWithClassName:@"ParseClass"];
query.skip = randomSkip;
query.limit = 1;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
PFObject *ParseClassObject = objects[0];
NSString *A = ParseClassObject[@"A"];
NSArray *B = ParseClassObject[@"B"];
completion(A, B);
} else {
NSLog(@"no error, but no ParseClass objects found");
}
} else {
NSLog(@"there was an error %@", error);
completion(nil, nil);
}
}];
}];


}





Aucun commentaire:

Enregistrer un commentaire