0

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",
3
  • 1
    Can we get an idea of what this JSON looks like? Commented Jul 17, 2014 at 15:48
  • 1
    Don't name methods with the prefix "get" unless they return values by reference. Commented Jul 17, 2014 at 15:53
  • thanks @Zaph, I just slapped it together for a test, it wasn't meant to be a permanent solution. Commented Jul 17, 2014 at 15:56

1 Answer 1

2

The problem is multithreading. _localChatters array is created asynchronously so in most cases this will happen after viewDidLoad. You could move [self getData] to dispatch_async block (before reloading table view and after fetching data from JSON).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.