Nick Langley
Nick Langley

Reputation: 39

Trouble getting parsed array in tableView iOS

How can I make sure that a query is finished (whether successful or not) before performing another task.

Here is my viewDidLoad...this line of code at the end

 NSLog(@"tweets Array %@",self.tweetsArray);

Is showing that tweetsArray is empty

Yet the same line of code in the block above it shows the Array to be full.

The Output also shows the last NSLog before the first.

My tableView is blank on the simulator because it is calling the UITableView methods before it has finished running query.

Any help on this would be great.

Thanks in Advance... Nick

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView.dataSource = self;
self.tableView.delegate = self;

self.selectString = self.gamesString;
PFQuery *query = [PFQuery queryWithClassName:@"Parse_Storyw"];
[query whereKey:@"searchName" equalTo:self.gamesString];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (!error) {
        // Do something with the found objects
        for (PFObject *object in objects) {
            NSLog(@"%@", object.objectId);


            PFQuery *query = [PFQuery queryWithClassName:@"Parse_Story"];
            [query getObjectInBackgroundWithId:object.objectId block:^(PFObject *content, NSError *error) {

                PFFile *file = [content objectForKey:@"levels"];

                [file saveInBackground];
                [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                    NSString *string = [[NSString alloc] initWithData: data  encoding: NSMacOSRomanStringEncoding];

                    self.tweetsArray = [[NSArray alloc] initWithArray:[string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];

                    NSLog(@"tweets Array %@",self.tweetsArray);


                }];
            }];
        }

    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

self.title = self.selectString;

NSLog(@"tweets Array %@",self.tweetsArray);

}

Upvotes: 0

Views: 281

Answers (2)

Anjaneyulu Battula
Anjaneyulu Battula

Reputation: 1960

  1. In your code Inside forloop you are keep upadating the self.tweetsArray

              self.tweetsArray = [[NSArray alloc] initWithArray:[string  componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
    

    in this line every time array loads with new data. So dynamically if you want to Change tableView Content use [yourTableView reloadData].

  2. If self.tweetsArray Content Modified then You have to Use [self.tableView reloadData] to Effect the changes in TableView.

Upvotes: 0

TamilKing
TamilKing

Reputation: 1643

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.


self.selectString = self.gamesString;
PFQuery *query = [PFQuery queryWithClassName:@"Parse_Storyw"];
[query whereKey:@"searchName" equalTo:self.gamesString];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

if (!error) {
    // Do something with the found objects
    for (PFObject *object in objects) {
        NSLog(@"%@", object.objectId);


        PFQuery *query = [PFQuery queryWithClassName:@"Parse_Story"];
        [query getObjectInBackgroundWithId:object.objectId block:^(PFObject *content, NSError *error) {

            PFFile *file = [content objectForKey:@"levels"];

            [file saveInBackground];
            [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                NSString *string = [[NSString alloc] initWithData: data  encoding: NSMacOSRomanStringEncoding];

                self.tweetsArray = [[NSArray alloc] initWithArray:[string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];

                NSLog(@"tweets Array %@",self.tweetsArray);
                self.tableView.dataSource = self;
                self.tableView.delegate = self;

            }];
        }];
    }

} else {
    // Log details of the failure
    NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];

self.title = self.selectString;

NSLog(@"tweets Array %@",self.tweetsArray);

Try this..

Upvotes: 1

Related Questions