So, I have a function referred to as "queryParseForRelevantData" This method queries parse and gets the data for the PF Objects and stores the data in NSMutable arrays. I would like one of these mutable arrays to then be displayed in a table. How can I get query to execute before the table is generated?
Here is the code I am using
#import "ResultsTableViewController.h"
#import "Parse/Parse.h"
@interface ResultsTableViewController ()
@end
@implementation ResultsTableViewController
{
}
- (void)viewDidLoad {
[self queryParseForRelevantData];
[super viewDidLoad];
_tableData = _flushTypeArray;
NSLog(@"why must you hate me %@",_dataPointNumberArray);
// NSLog(@"%@", _COMBINEDNAMES);
}
-(void)queryParseForRelevantData{
_dataPointNumberArray = [[NSMutableArray alloc] init];
_flushTypeArray = [[NSMutableArray alloc] init];
_tableData = [[NSMutableArray alloc] init];
_heightDifferenceArray = [[NSMutableArray alloc] init];
_variableArray = [[NSMutableArray alloc] init];
_massOfWaterArray = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:@"DataPoint"];
//queriess the class DataPoint to find when instances of "user" equal the
//logged in user's username
[query whereKey:@"userName" equalTo:_username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
NSLog(@"%@", objects);
NSLog(@"%@", objects.class);
NSLog(@"Successfully retrieved %lu users.", (unsigned long)objects.count);
// Do something with the found objects
if (objects.count == 0) {
//uialert letting the user know that no user matches the query
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Prior Data"
message:@"No Prior Data"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
//if there is only one number matching the query
if (objects.count >=1) {
for (PFObject *object in objects) {
// NSLog(@"%@", objects);
// NSLog(@"%@", object.class);
NSDictionary *dpn = object[@"dataPointNumber"];
NSDictionary *ft = object[@"flushType"];
NSDictionary *hd= object [@"heightDifference"];
NSDictionary *var = object [@"variable"];
NSDictionary *mow = object[@"massOfWater"];
if (dpn != NULL){
NSLog(@"Yo bro here is Dpn %@",dpn);
[_dataPointNumberArray addObject:dpn];
NSLog(@"number: %@", _dataPointNumberArray);
}
if (dpn == NULL) {
[_dataPointNumberArray addObject:@"Blank"];
NSLog(@"Blank space");
}
if (ft != NULL) {
[_flushTypeArray addObject:ft];
[_tableData addObject:ft];
NSLog(@"Flush Type: %@",_flushTypeArray);
NSLog(@"the table data is %@",_tableData);
}
if (ft == NULL) {
[_flushTypeArray addObject:@"Blank"];
NSLog(@"Blank space");
}
if (hd !=NULL){
[_heightDifferenceArray addObject:hd];
NSLog(@"height Difference: %@", _heightDifferenceArray);
}
if (hd ==NULL){
[_heightDifferenceArray addObject:@"blank"];
NSLog(@"Blank Space");
}
if (var!=NULL) {
[_variableArray addObject:var];
NSLog(@"Variable: %@", _variableArray);
}
if (var == NULL) {
[_variableArray addObject:@"blank"];
NSLog(@"Blank Space");
}
if (mow != NULL) {
[_massOfWaterArray addObject:mow];
NSLog(@"Mass of water: %@",_massOfWaterArray);
}
if (mow == NULL) {
[_massOfWaterArray addObject:@"blank"];
NSLog(@"Blank Space");
}
}
}
else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_tableData count];
// Return the number of rows in the section.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [_tableData objectAtIndex:indexPath.row];
return cell;
}
As DrGodCarl has said, it seems like you want to refresh the table once the information has been loaded. Typically this can be done in a number of ways through the use of blocks/completion handlers.
Here's an example using completion handlers:
- (void)queryParseForRelevantData:(void (^)(NSArray *))completionHandler
{
// Run your PFQuery's
PFQuery *query = [PFQuery queryWithClassName:@"DataPoint"];
[query whereKey:@"userName" equalTo:_username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Query found all your objects, return the completion handler
completionHandler(objects);
}
}
To invoke this method:
- (void)viewDidLoad
{
[self queryParseForRelevantData:^(NSArray *arrayOfDataPoints) {
// Update the table with the results.
}];
}
That way you can load the table only once the data has been retrieved from Parse.