I have an app, and I must get the info from Internet. The request to the url is in a block and this is made after that the UIViewController be appear, and that makes that my app crash on runtime because I use the info for construct the UIViewController.
I don't know how assure that the code block finish his tasks and later use the info.
*EDIT*
Now my app shows a empty table, and I dont know how make that shows the info that I get. Maybe could be with a reloadTable, but I don't know how. Help!
StoreController.m
- (void)viewDidLoad
{
[super viewDidLoad];
_productData = [ProductData ProductData];
[_productData backEndTest];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_listOfChips count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ProductCellController *cell = (ProductCellController*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSString* nameNib = UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM() ? @"ProductCellController" : @"ProductCellControllerIphone";
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nameNib owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell = (ProductCellController*)[self cellFormat:cell atTheIndex:indexPath withThelastIndex:pathToLastRow];
cell.image.image = [UIImage imageNamed:[((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])image]];
cell.title.text = [NSString stringWithFormat:@"%@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])title]];
cell.price.text = [NSString stringWithFormat:@"$ %@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])price]];
cell.amount.text = [NSString stringWithFormat:@"%@", [((Chip*)[[_productData listOfProducts] objectAtIndex:indexPath.row])quantity]];
}
ProductData.m
+(ProductData*)ProductData
{
ProductData* data = [[ProductData alloc]init];
return data;
}
- (void)backEndTest
{
[Server getProductsWithCompletionHandler:^(id result, NSError* error)
{
if ( error )
{
NSLog(@"error %@", error);
}
else
{
//NSMutableArray* arrayProducts = [NSMutableArray array];
int index = 0;
for (NSDictionary* product in result)
{
[_listOfProducts addObject:[[Products alloc] initWithProduct:[chip objectForKey:GSR_PARAM_PRODUCT_ID]
withTitle:[product objectForKey:GSR_PARAM_PRODUCT_DESCRIPTION]
numberUses:[product objectForKey:GSR_PARAM_PRODUCT_AMOUNT]
withPrice:[product objectForKey:GSR_PARAM_PRODUCT_PRICE]
withQuantity:[product objectForKey:GSR_PARAM_PRODUCT_AMOUNT]
withInAppID:[product objectForKey:GSR_PARAM_PRODUCT_IN_APP_ID]
withImage:[[self loadImages]objectAtIndex:index ]] ];
index++;
}
}
}];
}
I dont have any idea that how manage the block and assure his accomplish. Any ideas? Thanks in advance!