简体   繁体   中英

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]);
        }
    }];

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 = \\"\\";\\n variable1 = false;\\n moreStuff = \\"\\";\\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 = \\"\\";\\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.

Parse has special User class for user accounts. Your problem is not clear, but if you want to store received objects in NSArray you create an NSMutableArray copy of current array, add objects using [array addObject:object] method and assign its value to original array.

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