2014-03-18 2 views
0

TableView가 처음 시작되면 JSON을 통해 데이터를 검색해야합니다. 불행히도 이것은 작동하지 않습니다. 새로 고침을 사용할 때만 데이터가로드됩니다. 누군가가 내가 문제를 찾도록 도와 줄 수 있습니까? 도와 주셔서 감사합니다.NSURLConnection sendAsynchronousRequest

- (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]; 


} 

답변

0

을 이름이 sendAsynchronousRequest가 비동기 있듯이. 즉 함수 자체가 작업을 완료하지 않은 경우에도 함수는 다음 코드 줄을 반환하고 실행을 계속합니다. 이것은 또한 당신이 이미 reloadData 방법을 실행 한 경우 요청이 여전히 따라서 다시로드 데이터없이 작동하는지 가능성 때문에 요청이 여전히를 받고 있기 때문에이 라인

[self.tableView reloadData]; 

가, 아무런 효과가 없다는 것을 의미 정보.

두 번째로 작동하지만 실제로 작동하지 않습니다. 두 번째로 보는 것은 이전 요청의 데이터입니다.

한 가지 가능한 해결책은 위에있는 completionHandler 블록 안에 reloadData 행을 배치하는 것입니다.

+0

도움 주셔서 감사합니다. 이제 작동합니다. – carlie

+0

당신을 진심으로 환영합니다. 체크 표시로 대답을 수락하면 감사하겠습니다. – Merlevede

+0

그리고 "Ergebniss1"을 "Ergebnis1"등으로 바꾸실 수 있습니까? 독일어를 배우려는 사람들이이 책을 읽고 잘못된 맞춤법을 배우기 시작할 수도 있습니다. – gnasher729

0

비동기 완료 블록의 테이블에서 reloadData을 호출하지 않습니다. 비동기 호출을 한 직후에 호출합니다. 대신이 시도 :

// 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]; 
    } 
}]; 
관련 문제