Studio Symposium
Studio Symposium

Reputation: 287

Accessing Items in NSArray from PFObject

I'm building an application on Parse.com's backend for iOS that has simple follower / following logic to it. I.e. I want the user to be able to find other users and follow their profiles. I can currently retrieve all of the users in the database using a general PFQuery. Tapping on a users profile image "follows" that person. The data is set up so that an array called isFollowing has a unique object added to it with each new person that the user chooses to follow. It's adding it by the users objectId.

However, here's where my bump in the road is; for a separate screen I only want to return the number of people I, as a user, am following. This is the query to access the data:

- (void)viewDidAppear:(BOOL)animated
{
    PFQuery *followingQuery = [PFUser query];
    [followingQuery whereKey:@"objectId" equalTo:[PFUser currentUser].objectId];
    [followingQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error)
        {
            NSLog(@"User test: %@", [objects valueForKey:@"isFollowing"]);
            PFQuery *imagesQuery = [PFUser query];
            [imagesQuery whereKey:@"objectId" containedIn:[objects valueForKey:@"isFollowing"]];
            [imagesQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
            {
                if (!error)
                {
                    NSLog(@"Username? %@", [objects valueForKey:@"username"]);
                }
                else
                {
                    NSLog(@"Something went wrong. %@", error);
                }
            }];
        }
        else
        {
            NSLog(@"Something went wrong: %@", error);
        }
    }];
    [super viewDidAppear:animated];
}

Currently, user test: %@ gives the output:

2014-07-28 10:17:58.537 YouStreamSport[11276:60b] User test: ( ( bHul1vkkmB, brsN8PRUBO ) )

However, when I run objects.count it's returning one object. Because of this I'm unable to iterate through the array object to find and return only the proper profile images. Since the array object is returning as 1, but there are 2 objects in the array itself, how can I access the items in that array? Any attempts to access using [objectAtIndex: ...] result in a crash.

Thanks for the input in advance.

Upvotes: 2

Views: 888

Answers (2)

Automate This
Automate This

Reputation: 31364

As written you will only display one objects username. Instead of trying to access them by index try iterating through the results like below. I'd also recommend not using the variable objects for both the inner and outer query. Try this out:

[imagesQuery findObjectsInBackgroundWithBlock:^(NSArray *myObjects, NSError *error)
{
  if (!error){
    if (myObjects.count) {
      for (PFObject *item in myObjects){
        NSLog(@"Username: %@", [item valueForKey:@"isFollowing"]);
      }
    }
  }else{
    NSLog(@"Something went wrong. %@", error);
  }
}];

Upvotes: 1

Studio Symposium
Studio Symposium

Reputation: 287

I wasn't sure how to access a single item in an array within an array, but thanks to the help of @Droppy, I was able to solve the problem. This is the code I am now using:

- (void)viewDidAppear:(BOOL)animated
{
PFQuery *followingQuery = [PFUser query];
[followingQuery whereKey:@"objectId" equalTo:[PFUser currentUser].objectId];
[followingQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error)
    {
        self.array = [objects valueForKey:@"isFollowing"];
        NSLog(@"User test: %@", self.array);
        for (int i = 0; i <= self.array.count; i++)
        {
            NSLog(@"User %d: %@", i, [[[objects valueForKey:@"isFollowing"] objectAtIndex:0] objectAtIndex:i]);
        }
    }
    else
    {
        NSLog(@"Something went wrong: %@", error);
    }
}];
[super viewDidAppear:animated];
}

The resulting output is as follows:

2014-07-28 10:53:16.155 appName[11416:60b] User 0: bHul1vkkmB 2014-07-28 10:53:16.156 appName[11416:60b] User 1: brsN8PRUBO

Thank you!

Upvotes: 2

Related Questions