简体   繁体   中英

How to run a loop of parse queries?

I am making an app that registers users and allows them to add friends etc. So I have a LoginViewController where I retrieve the array of user's friends' objectIds when the login is successful. This function is called.

- (void) getFriendList
{
    NSString * objectID = [PFUser currentUser].objectId;
    NSLog(@"%@", objectID);
    PFQuery *query = [PFUser query];
    [query getObjectInBackgroundWithId:objectID block:^(PFObject *username, NSError *error) {
        sharedClass.sharedInstance->retrievedFriends = username[@"friendsIds"];
        friendListLoaded = YES;
        [self getFriendsUsernames];  
    }];

Here i get an array object that contains object ids of all the friends this user has. As you can see when getFriendList is completed, it calls another function called GetFriendsUsernames. This function is supposed to retrieve profile pictures and usernames of those friends so I can populate the Friend List view later.

-(void) getFriendsUsernames
{
    NSMutableArray * objectIDs = [[NSMutableArray alloc] initWithArray: sharedClass.sharedInstance->retrievedFriends];
    PFQuery *query = [PFUser query];
    int friendsCount = [objectIDs count];
    for(int i = 0; i<=friendsCount;i++)
    {
        [query getObjectInBackgroundWithId:objectIDs[i] block:^(PFObject *username, NSError *error) {
            [sharedClass.sharedInstance->friendsUsernames addObject:username[@"username"]];
            [sharedClass.sharedInstance->friendsProfilePictures addObject:username[@"ProfilePicture"]];

        }];
        NSLog(@"%@", sharedClass.sharedInstance->friendsUsernames );
    }

}

But this seems to be unsuccessful because nothing is logged on the console where it should log username of retrieved friend whenever one query gets finished. Am I doing this the right way? My array count is right so loop runs to the number of friends a user has. It also prints the username of first object in objectIds array if i put the logging command in the loop.

Your NSLog runs immediately after your query objects are queued for execution. Therefore, even if the code is running correctly (and I suspect it might not be), you'll never get the correct results you're after logged to the console.

If you have your Parse classes designed in a certain way, you could collapse this into 1 query by using the include method on PFQuery . This assumes that you've created an array of Pointers on your User object, named "friends". If you actually store the objectId (ie, the string value) of each friend, this code won't work.

I'm not using Swift yet, so here's how I'd write this query in Objective-C:

- (void)getFriendList {
    PFUser *currentUser = [PFUser currentUser];
    PFQuery *query = [PFUser query];
    [query whereKey:@"objectId" equalTo:currentUser.objectId];
    [query includeKey:@"friends.username"];
    [query includeKey:@"friends.ProfilePicture"];
    [query getFirstObjectInBackgroundWithBlock:^(PFObject *user, NSError *error) {
        if (error != nil) {
            // Process the error

        } else }
            // You've got your user and the data you wanted

        }
     }];
}

Found it. Not the prettiest way out there but well it does what i want. gets me arrays of usernames and profile pictures of every objectID contained in array of friends.

 NSMutableArray * objectIDs = [[NSMutableArray alloc] initWithArray: sharedClass.sharedInstance->retrievedFriends];
    PFQuery *query = [PFUser query];
    PFFile * imageFile;
    //UIImage *profilePictureData;
    int friendsCount = [objectIDs count];
    for(int i = 0; i<friendsCount;i++)
    {
        PFObject * username =  [query getObjectWithId:objectIDs[i]];
        [sharedClass.sharedInstance->friendsUsernames addObject:username[@"username"]];
        [sharedClass.sharedInstance->friendsEmailAdresses addObject:username[@"email"]];
        //NSLog(@"%@",username[@"ProfilePicture"]);
        imageFile = [username objectForKey:@"ProfilePicture"];
        NSData *imageData = [imageFile getData];
        UIImage *imageFromData = [UIImage imageWithData:imageData];
        [sharedClass.sharedInstance->friendsProfilePictures addObject:imageFromData];
        NSLog(@"%@", sharedClass.sharedInstance->friendsUsernames );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM