iosobjective-cparse-platformnsarray

Parse PFQuery to array objective-c


All the functionality is based on the Parse. I have a class called "Connect", which contains user accounts. My code gets the record and displays them in NSLog. But I need to he led them into the NSArray. My NSArray called userPost and userName.

PFQuery *query = [PFQuery queryWithClassName:@"Connect"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            NSLog(@"Successfully retrieved %lu messages.", (unsigned long)objects.count);
            for (PFObject *object in objects) {
                NSLog(@"object with id has location %@ %@", object.objectId, artistName);
                NSLog(@"object with id has location %@ %@", object.objectId, artistMessage);
            }
        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

Solution

  • The only problem with my first solution is that you would need to setup an NSObject subclass model that describes the class that you have on Parse.com and the load each dictionary from the response array into a new object for each dictionary that you query. You can pull down the entire array, but just so you know, that array contains a ton of info that is nested if you have a class on Parse.com that is interconnected to other tables through pointers.

    Actually, here's the code you'd need, I'm just going to post the entire code set: in the header file, declare a property like so:

    @property (nonatomic, strong)NSArray *ninjas;
    
    - (void)loadNinjas
    {
        PFQuery *query = [PFQuery queryWithClassName:@"stuff"];
        [query whereKey:@"userId" equalTo:[PFUser currentUser]];
        [query includeKey:@"moreStuff"];
        [query findObjectsInBackgroundWithBlock:^(NSArray *a, NSError *error) {
            if (!error) {
                NSMutableArray *tempNinjas = [[NSMutableArray alloc] init];
                for (PFObject *dic in a) {
                    
                    NSHStuff *ninja = [[NSHStuff alloc] initWithDictionary:dic];
                    [tempNinjas addObject:ninja];
                    
                }
                self.ninjas = [[NSArray alloc] initWithArray:tempNinjas];
                tempNinjas = nil;
                dispatch_async(dispatch_get_main_queue(), ^ {
                    [self.contentView.tableView reloadData];
                });
            }
        }];
    }
    

    just get the Array of objects:

    - (void)loadNinjas
    {
        PFQuery *query = [PFQuery queryWithClassName:@"stuff"];
        [query whereKey:@"userId" equalTo:[PFUser currentUser]];
        [query includeKey:@"moreStuff"];
        [query findObjectsInBackgroundWithBlock:^(NSArray *a, NSError *error) {
            if (!error) {
               
                self.ninjas = a;
            }
        }];
    }
    

    Here's what the innards of the returned array looks like:

    stuff: 0x7fcf15avv3af1e0, objectId: L4adsfeafeDHSky, localId: (null)> {\n    ACL = \"<PFACL: 0x7fafcf15c99540>\";\n    variable1 = false;\n    moreStuff = \"<photosForVariableImage: 0x7fcf15cadfds1e0, objectId: 7HcnpqafV6TW>\";\n    counter1 = 0;\n    justatextbodyfortesting = \"Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test Test test test test test test test test test test test test test test test test test test test test test test test \";\n    \"anotherconter\" = 0;\n    title = \"This is the title\";\n    userId = \"<PFUser: 0x7fcf15c37878, objectId: FnbWf9nfhSf>\";\n}",
    

    the above "stuff" represents 1 row of a table in Parse with all the pointer information connected.

    You will have the same "stuff" like this a bunch of times for each row that you have in your table, this means that the returned array from Parse has a ton of dictionaries and each dictionary contains the entire row for the table you are querying.