0

I'm having issues getting data from the pointers within objects I get from from my parse query. I use the include key on the query, but having issues getting the include key information from the returned objects array. I created a for loop to iterate over the results but not sure how to store them back into an array. Here's my code below:

- (void) loadUsersPFUserIsFollowing
    {


        //Retrieving PFUser Curren Users followers and storing in Array
        PFQuery *queryFollowingCount = [PFQuery queryWithClassName:@"Activity"];
        [queryFollowingCount whereKey:@"type" equalTo:@"follow"];
        [queryFollowingCount whereKey:@"fromUser" equalTo:[PFUser currentUser]];

        //Access the the user following informatioin
        [queryFollowingCount includeKey:@"toUser"];

        //[queryFollowingCount setCachePolicy:kPFCachePolicyCacheThenNetwork];

        [queryFollowingCount findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            [HUD hide:YES];
            if (!error) {

               searchResultsArray = [[NSArray alloc] initWithArray:objects];            

               NSLog(@"%@", objects);

                for (PFObject * postObject in objects) {

                   PFObject *postAuthor = [postObject objectForKey:@"toUser"][@"username"];



                    NSLog(@"retrieved related Post Author: %@", postAuthor);
                }

                  [self.tableView reloadData];

            } else {
                NSLog(@"Something went wrong");
                NSLog(@"%@", error);
            }
        }];

    }

1 Answer 1

2
for (PFObject * postObject in objects) {
     PFObject *postAuthor = [postObject objectForKey:@"toUser"][@"username"];

     NSLog(@"retrieved related Post Author: %@", postAuthor);
}

By default, when fetching an object, related PFObjects are not fetched. These objects' values cannot be retrieved until they have been fetched like so:

for (PFObject * postObject in objects) {

     //Im assuming in your code, postObject is either nil or it's there but it's values are nil except its objectId. We need to fetch this object. 
     [postObject fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error){

          PFObject *postAuthor = [object objectForKey:@"toUser"][@"username"];

          NSLog(@"retrieved related Post Author: %@", postAuthor);

         [self.tableView reloadData];

      }];
}
Sign up to request clarification or add additional context in comments.

3 Comments

ahhhh gotcha! I have a search implemented that will filter through the returned results. I'm trying to store each fetch pfobject into an array so that I can filter through the results using a search , but I see the fetchIf.. only returns one object. Or am I reading this wrong?
Your initial query queryFollowingCount will return the top level object and it's properties. The pointers to other objects are there but faulted, so it's just a bunch of objectIds of the PFObjects your top level object is pointing to. You have to fetch each one of those objects you point to independently.
Gotcha, I'll have to refine my query to a compound query to get the results I'm looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.