In order to test my App I have created a JSON file that I am drawing fake values from, however in my table View I want to exclude the user's data. In order to load this file I have used this code:
//create a new JSONLoader with a local file from URL
JSONLoader *jsonLoader = [[JSONLoader alloc] init];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"chatters" withExtension:@"json"];
//load the data on a background queue
//use for when connecting a real URL
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_localChatters = [jsonLoader chattersFromJSONFile:url];
//push data on main thread (reload table view once JSON has arrived)
//[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
});
Then I load it into a tableView with no problems:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"PopulationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Chatter *chatter = [_localChatters objectAtIndex:indexPath.row];
NSData *fbImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: chatter.url]];
UIImage *profilePicture = [UIImage imageWithData:fbImageData];
cell.imageView.image =profilePicture;
[cell.textLabel setAdjustsFontSizeToFitWidth: NO];
cell.textLabel.text = [NSString stringWithFormat:@"%@ joined at %@",chatter.name,chatter.joined];
cell.textLabel.textColor = [UIColor grayColor];
return cell;
}
However this includes the user information as well, which is something we don't want. In order to get around this problem I have created a separate method that would in theory create a second mutable array with just the data excluding the user's:
- (void)getData{
NSLog(@"%@",_localChatters);
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i<[_localChatters count]; i++) {
Chatter *newChatter = [_localChatters objectAtIndex:i];
if ([newChatter.facebookID isEqualToString:_loggedInFBID]) {
} else {
[newArray addObject:newChatter];
};
}
NSLog(@"%@",newArray);
}
However when this method is called from the view did load with a
[self getData]
the _localChatters NSLogs as (null) and I believe subsequently the newArray is never filled and NSLogs as empty (). It is strange because when I log the _localChatters in the uitableview it is not null and of course all of the data is there when it loads. I'm at a loss why the _localChatters would read as null in this method when I am fairly certain the array loads just fine from the dispatch_async request.
EDIT:
Here is a small sample of JSON, I've removed some of the personal information such as FB ID and picture URL, however all of the objects are the same.
"name": "Johnny",
"room": "London",
"latitude": 41.414483,
"longitude": 2.152579,
"message": "I agree, I think I'm going there right now",
"timestamp": "9:23 PM",
"url": " <<actual FB profile URL>>",
"facebookID":"<<personal FB ID >>",
"joined":"12:13 AM",