In a tableViewController of users I have this inside an NSOperationQueue call which is successfully getting the data and creating the users.Array:
Code:
//GET user/points ARRAY
for (NSDictionary *usersDataDictionary in self.usersArray) {
// call the method to get that user's points from a web service call
[self getMyPoints:[usersDataDictionary objectForKey:@"username"]];
}
getMyPoints is a void method that uses ASI HTTP to asynchronously make a request to a php web service:
and returns the data in a responseString through a delegate method like this:
Code:
- (void)requestFinished:(ASIHTTPRequest *)request{
// Receive the request from the server
NSString *responseString = [request responseString];
NSLog(@"string received is:%@",responseString);
NSError *outError = NULL;
// Place that json String into a mutable array
[ self.myPoints addObject:[NSDictionary dictionaryWithJSONString:responseString error:&outError ]];
NSLog(@"the myPointsArray is:%@", myPoints );
[self.tableView reloadData];
}
I will then use that mutable array to populate the detailText in a tableview cell.
Everything works fine up to where I print out the responseString. But from that point on, the parsed string doesn't get put into the myPoints array because that array returns (NULL) when logged into the console.
In a tableViewController of users I have this inside an NSOperationQueue call which is successfully getting the data and creating the users.Array:
Code:
//GET user/points ARRAY
for (NSDictionary *usersDataDictionary in self.usersArray) {
// call the method to get that user's points from a web service call
[self getMyPoints:[usersDataDictionary objectForKey:@"username"]];
}
getMyPoints is a void method that uses ASI HTTP to asynchronously make a request to a php web service:
and returns the data in a responseString through a delegate method like this:
Code:
- (void)requestFinished:(ASIHTTPRequest *)request{
// Receive the request from the server
NSString *responseString = [request responseString];
NSLog(@"string received is:%@",responseString);
NSError *outError = NULL;
// Place that json String into a mutable array
[ self.myPoints addObject:[NSDictionary dictionaryWithJSONString:responseString error:&outError ]];
NSLog(@"the myPointsArray is:%@", myPoints );
[self.tableView reloadData];
}
I will then use that mutable array to populate the detailText in a tableview cell.
Everything works fine up to where I print out the responseString. But from that point on, the parsed string doesn't get put into the myPoints array because that array returns (NULL) when logged into the console.
Any Ideas?
Are you using ARC, or retain/release?
Where is the array myPoints being allocated? It looks like it's a property of your class. Post the code that creates the array and saves it to the property.
You should have the property set up as retain (or strong, if you're using ARC) and make sure you assign the array to the property using the setter. Something like this:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Then just now I did this, to make sure what was being put into the array:
Code:
// I COMMENTED THIS PART OUT
//[self.myPoints addObject:
// IN ORDER TO LOG WHAT WILL BE PUT INSIDE...
NSLog(@"parsed json.........%@",[NSDictionary dictionaryWithJSONString:responseString error:&outError]);
and i correctly get this:
2011-11-21 16:07:35.487 [925:15b03] string received is:[{"username":"IrishSparky","PUNTOS":"2"}]
2011-11-21 16:07:35.488 [925:15b03] parsed json.........(
{
PUNTOS = 2;
username = IrishSparky;
}
)
Think about what that code does! It creates a new mutable array and assigns it to your myPoints property.
Then, on the very next line, you set self.myPoints back to nil, so of course you can't save values to the array. The array is gone, almost as soon as its created!
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Ok i tried both things. Now it crashes a bit further down because the self.myPoints array is empty when CFRAIP is called because here is the complete code:
Code:
if(indexPath.section == 0){
// Configure the cell with players in game.
NSDictionary *userDataDict = [usersArray objectAtIndex:indexPath.row];
cell.textLabel.text = [userDataDict objectForKey:@"username"];
// NOW DISPLAY POINTS
//NSArray *userPointsArray = [self.myPoints objectAtIndex:indexPath.row];
//NSDictionary *userPointsDict = [userPointsArray objectAtIndex:0];
//NSString *pts = [userPointsDict objectForKey:@"POINTS"];
//cell.detailTextLabel.text = pts;
return cell;
}
else {
//IF NOT FIRST FEW CELLS...
// Configure cell with your stats
NSLog(@"1st object:%@",[self.myPoints objectAtIndex:0]);
NSDictionary *dict = [self.myPoints objectAtIndex:0];
NSLog(@"1st key:%@",[dict objectForKey:@"PUNTOS"]);
cell.textLabel.text = [dict objectForKey:@"PUNTOS"];
cell.detailTextLabel.text = @"Bumped tokens are worth more!";
cell.textLabel.backgroundColor=[UIColor clearColor];
return cell;
}
I guess its reaching the CFRAIP method before the NSOperationQueue is done...because this is the error I get on crash:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
----------EDIT----------
So I added a @"hi" string to the array and it worked but it crashed later on because of course, its not a dict I'm passing it, it was just a string. Anywho, so what's happening is that since Im separating the getting of that data into a separate NSOperation, it keeps running the CFRAIP method and of course reaches an empty self.myPoints and crashes...
This means I have to rethink my logic here...Im thinking of adding an if/else such that if the self.myPoints array is still NULL then just add placeholder text...until the array has been populated...like: