0

Im parsing JSON data in an external class (NSObject) class that then put organizes the parsed data into an array. That array is then placed into my Table View. Because it takes longer to parse the info then to finish viewDidLoad the array is always set to nil.

Code:

- (void)viewDidLoad
{
//Fetching Data
[super viewDidLoad];
myClass = [[MyClass alloc] init];
[myClass fetchDataWithTarget:self];

//Initialize the dataArray
dataArray = [[NSMutableArray alloc] init];

//First section data
NSArray *firstItemsArray = myClass.fechedDataArray;
NSDictionary *firstItemsArrayDict = [NSDictionary dictionaryWithObject:firstItemsArray forKey:@"data"];
[dataArray addObject:firstItemsArrayDict];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

NSDictionary *dictionary = [dataArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"data"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
}  

How can I insure that firstItemsArray is set after the data is fetched.

Note: There is obviously more to the table view then this, I did all the dictionary stuff to separate sections and what not.

Tanner
  • 183
  • 1
  • 1
  • 7
  • I do not see any asynchronous methods in your code, so what exactly do you mean with *"after the data is fetched"*? – Martin R Jun 11 '13 at 01:24
  • What does `fetchDataWithTarget` do? – tia Jun 11 '13 at 01:26
  • 1
    A very similar [question](http://stackoverflow.com/q/16315317/767730). I have given an [answer](http://stackoverflow.com/a/16315573/767730) which you can use in this case as well. – Anupdas Jun 11 '13 at 01:27

1 Answers1

0

You'll have to load the JSON data into the array BEFORE

- (void) viewDidLoad

or use some type threading (asynchronous method) such as:

performSelectorInBackground:withObject

NSOperation or Grand Central Dispatch

race_carr
  • 1,387
  • 12
  • 21