0

if the TableView starts for the first time, he should retrieve data via JSON. Unfortunately this does not work. Only when I use refresh, the data is loaded. Can someone help me find the problem. Thanks for the help.

- (void)viewDidLoad
{
[super viewDidLoad];

UINib *nib = [UINib nibWithNibName:@"ErgebnissCell" bundle: nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"eCell"];


UIRefreshControl *theControl = self.refreshControl;

if([theControl actionsForTarget:self forControlEvent:UIControlEventValueChanged] == nil) {
    [theControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
}
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Aktuallisiere"];

switch ([self checkInternetConnection]) {
    case NotReachable:
        NSLog(@"Keine Internetverbindung Vorhanden");
        break;
    case ReachableViaWiFi:
        NSLog(@"WI-FI Verbindung Vorhanden");
        break;
    case ReachableViaWWAN:
        NSLog(@"WWAN Verbindung vorhanden");
        break;

    default:
        break;

       }

[self updateItems];

}

-(void)stopRefresh
{
[self.refreshControl endRefreshing]; 
}



- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

-(NetworkStatus)checkInternetConnection
{
self.reachability = [Reachability reachabilityForInternetConnection];
return [self.reachability currentReachabilityStatus];
}

-(NetworkStatus)checkInternetConnectionWithHostName:(NSString*)hostName
{
self.reachability = [Reachability reachabilityWithHostName:(NSString *)hostName];
return [self.reachability currentReachabilityStatus];
}

- (void)viewWillAppear:(BOOL)inAnimated {
[super viewWillAppear:inAnimated];
[self updateItems];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)inInterfaceOrientation {
return YES;
}

-(IBAction)refresh:(id)sender
{
[self updateItems];
}


- (void)updateItems {

NSString *ergbnisseURL = @"http://iosentwicklung.de/Ergebnisse_2014.json";
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:ergbnisseURL]];
[NSURLConnection sendAsynchronousRequest: theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *allCoursesData, NSError *error) {
    if (allCoursesData == nil) {
        NSLog(@"error = %@", error);
    }
    else {
        NSError *error;
        self.alleErgebnisse = [NSJSONSerialization
                               JSONObjectWithData:allCoursesData
                               options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
                               error:&error];

        self.heim1 = self.alleErgebnisse[@"Heim1"];
        self.gast1 = self.alleErgebnisse[@"Gast1"];
        self.ergebniss1 = self.alleErgebnisse[@"Ergebniss1"];
        self.heim2 = self.alleErgebnisse[@"Heim2"];
        self.gast2 = self.alleErgebnisse[@"Gast2"];
        self.ergebniss2 = self.alleErgebnisse[@"Ergebniss2"];
        self.heim3 = self.alleErgebnisse[@"Heim3"];
        self.gast3 = self.alleErgebnisse[@"Gast3"];
        self.ergebniss3 = self.alleErgebnisse[@"Ergebniss3"];
        self.heim4 = self.alleErgebnisse[@"Heim4"];
        self.gast4 = self.alleErgebnisse[@"Gast4"];
        self.ergebniss4 = self.alleErgebnisse[@"Ergebniss4"];
        self.heim5 = self.alleErgebnisse[@"Heim5"];
        self.gast5 = self.alleErgebnisse[@"Gast5"];
        self.ergebniss5 = self.alleErgebnisse[@"Ergebniss5"];
        self.heim6 = self.alleErgebnisse[@"Heim6"];
        self.gast6 = self.alleErgebnisse[@"Gast6"];
        self.ergebniss6 = self.alleErgebnisse[@"Ergebniss6"];
        self.heim7 = self.alleErgebnisse[@"Heim7"];
        self.gast7 = self.alleErgebnisse[@"Gast7"];
        self.ergebniss7 = self.alleErgebnisse[@"Ergebniss7"];
        self.heim8 = self.alleErgebnisse[@"Heim8"];
        self.gast8 = self.alleErgebnisse[@"Gast8"];
        self.ergebniss8 = self.alleErgebnisse[@"Ergebniss8"];
        self.heim9 = self.alleErgebnisse[@"Heim9"];
        self.gast9 = self.alleErgebnisse[@"Gast9"];
        self.ergebniss9 = self.alleErgebnisse[@"Ergebniss9"];
        self.heim10 = self.alleErgebnisse[@"Heim10"];
        self.gast10 = self.alleErgebnisse[@"Gast10"];
        self.ergebniss10 = self.alleErgebnisse[@"Ergebniss10"];
        self.heim11 = self.alleErgebnisse[@"Heim11"];
        self.gast11 = self.alleErgebnisse[@"Gast11"];
        self.ergebniss11 = self.alleErgebnisse[@"Ergebniss11"];
        self.heim12 = self.alleErgebnisse[@"Heim12"];
        self.gast12 = self.alleErgebnisse[@"Gast12"];
        self.ergebniss12 = self.alleErgebnisse[@"Ergebniss12"];
        self.heim13 = self.alleErgebnisse[@"Heim13"];
        self.gast13 = self.alleErgebnisse[@"Gast13"];
        self.ergebniss13 = self.alleErgebnisse[@"Ergebniss13"];
        self.heim14 = self.alleErgebnisse[@"Heim14"];
        self.gast14 = self.alleErgebnisse[@"Gast14"];
        self.ergebniss14 = self.alleErgebnisse[@"Ergebniss14"];
        self.heim15 = self.alleErgebnisse[@"Heim15"];
        self.gast15 = self.alleErgebnisse[@"Gast15"];
        self.ergebniss15 = self.alleErgebnisse[@"Ergebniss15"];
        self.heim16 = self.alleErgebnisse[@"Heim16"];
        self.gast16 = self.alleErgebnisse[@"Gast16"];
        self.ergebniss16 = self.alleErgebnisse[@"Ergebniss16"];
        self.heim17 = self.alleErgebnisse[@"Heim17"];
        self.gast17 = self.alleErgebnisse[@"Gast17"];
        self.ergebniss17 = self.alleErgebnisse[@"Ergebniss17"];
        self.heim18 = self.alleErgebnisse[@"Heim18"];
        self.gast18 = self.alleErgebnisse[@"Gast18"];
        self.ergebniss18 = self.alleErgebnisse[@"Ergebniss18"];
        self.heim19 = self.alleErgebnisse[@"Heim19"];
        self.gast19 = self.alleErgebnisse[@"Gast19"];
        self.ergebniss19 = self.alleErgebnisse[@"Ergebniss19"];
        self.heim20 = self.alleErgebnisse[@"Heim20"];
        self.gast20 = self.alleErgebnisse[@"Gast20"];
        self.ergebniss20 = self.alleErgebnisse[@"Ergebniss20"];
        self.heim21 = self.alleErgebnisse[@"Heim21"];
        self.gast21 = self.alleErgebnisse[@"Gast21"];
        self.ergebniss21 = self.alleErgebnisse[@"Ergebniss21"];
        self.heim22 = self.alleErgebnisse[@"Heim22"];
        self.gast22 = self.alleErgebnisse[@"Gast22"];
        self.ergebniss22 = self.alleErgebnisse[@"Ergebniss22"];
        self.heim23 = self.alleErgebnisse[@"Heim23"];
        self.gast23 = self.alleErgebnisse[@"Gast23"];
        self.ergebniss23 = self.alleErgebnisse[@"Ergebniss23"];
        self.heim24 = self.alleErgebnisse[@"Heim24"];
        self.gast24 = self.alleErgebnisse[@"Gast24"];
        self.ergebniss24 = self.alleErgebnisse[@"Ergebniss24"];
        self.heim25 = self.alleErgebnisse[@"Heim25"];
        self.gast25 = self.alleErgebnisse[@"Gast25"];
        self.ergebniss25 = self.alleErgebnisse[@"Ergebniss25"];

    }
}];
[self.tableView reloadData];
[self.refreshControl setTintColor:[UIColor colorWithRed:0.000 green:0.000 blue:0.630 alpha:1.000]];
//[self setRefreshControl: self.refreshControl];
[self performSelector:@selector(stopRefresh) withObject:nil afterDelay:3];
//[self.refreshControl endRefreshing];


}
Aaron
  • 7,055
  • 2
  • 38
  • 53
carlie
  • 135
  • 1
  • 1
  • 9

2 Answers2

0

You're not calling reloadData on the table from the async completion block. You're calling it immediately after making the asynchronous call. Try this instead:

// weak reference self:
__weak typeof(self)weakSelf = self;

[NSURLConnection sendAsynchronousRequest: theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *allCoursesData, NSError *error) {

    __strong typeof(weakSelf)strongSelf = weakSelf;

    if (allCoursesData == nil) {
        NSLog(@"error = %@", error);
    }
    else {
        NSError *error;
        self.alleErgebnisse = [NSJSONSerialization
                           JSONObjectWithData:allCoursesData
                           options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
                           error:&error];

        // do stuff 

        // RELOAD TABLE HERE:
        [self.tableView reloadData];
    }
}];
Aaron
  • 7,055
  • 2
  • 38
  • 53
0

As the name implies sendAsynchronousRequest is asynchronous. This means that the function returns and keeps executing the next line of code, even if the function itself hasn't finished its job. This also means that this line

[self.tableView reloadData];

has no effect, because it's very likely that the request is still working when you already have executed the reloadData method, hence without any data to reload, because the request is still getting the information.

Also, the second time it works, but in fact it is not working. What you see the second time, is the data from the previous request.

One possible solution would be placing the reloadData line inside the completionHandler block, right above.

Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • And can you please change "Ergebniss1" to "Ergebnis1" etc.? There might be people reading this who try to learn German, and start learning incorrect spellings. – gnasher729 Mar 18 '14 at 22:19