[問題] 有關使用NSURLSession抓Json資料的問題

看板MacDev作者 (strifecloud)時間9年前 (2016/04/16 19:16), 編輯推噓3(3020)
留言23則, 6人參與, 最新討論串1/2 (看更多)
大家好,小弟最近想自己試著使用NSURLSession來寫寫看如何抓到JSON的資料 主要架構就是用一個TableViewControler把抓回來的JSON資料一筆一筆顯示出來 上網查了許多資料自己試做後,有成功抓到JSON資料, 但是目前遇到問題是資料回來之前, TableViewController 就已經建立好了, 所以一開APP不會顯示資料,我需要等資料回來後, 準備一個執行[TableView reloadData]的方法的按鈕, 按下後才能在TableView上顯示資料。 我參照網路上以及Apple的一些官方sample, 把抓資料的NSURLSession寫在AppDelegate的Class裡 可能應為對於能在哪個時機點讓APP自動執行reloadData還不是很了解, 想請教各位大大,感恩! 程式碼如下(目前的寫法曾經有幾次成功在APP執行後自動顯示,但成功機率很低..): static NSString * const taipeiUrl = @"http://data.taipei.gov.tw/opendata/apply/json/RkRDNjk0QzUtNzdGRC00ODFCLUJBNDktNEJCMUVCMDc3ODFE"; @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:taipeiUrl] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){ NSLog(@"error = %@", [error description]); if (error != nil) { [[NSOperationQueue mainQueue] addOperationWithBlock: ^{ [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; if ([error code] == NSURLErrorAppTransportSecurityRequiresSecureConnection) { abort(); } else { [self handleError:error]; } }]; } else{ NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; self.newsArray = [json valueForKey:@"name"]; NSLog(@"%@", self.newsArray); __weak AppDelegate *weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^(void){ NewsTableViewController *rootViewController = (NewsTableViewController *)[(UINavigationController *)weakSelf.window.rootViewController topViewController]; [rootViewController.tableView reloadData]; }); // show in the status bar that network activity is starting [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } }]; [dataTask resume]; //UI self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //create the navigation controller and add the controllers view to the window self.navigationController = [[UINavigationController alloc] init]; [self.window addSubview:[self.navigationController view]]; NewsTableViewController *newsViewController = [[NewsTableViewController alloc] initWithNibName:@"NewsTableViewController" bundle:nil]; //push the first viewcontroller into the navigation view controller stack [self.navigationController pushViewController:newsViewController animated:YES]; self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = self.navigationController; [self.window makeKeyAndVisible]; // show in the status bar that network activity is starting [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; return YES; } // handleError:error // Reports any error with an alert which was received from connection or loading failures. - (void)handleError:(NSError *)error { NSString *errorMessage = [error localizedDescription]; // alert user that our current record was deleted, and then we leave this view controller // UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Cannot Show Top Paid Apps" message:errorMessage preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // dissmissal of alert completed }]; [alert addAction:OKAction]; [self.window.rootViewController presentViewController:alert animated:YES completion:nil]; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.12.200 ※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1460805376.A.988.html

04/16 19:21, , 1F
reload的時機點沒錯 但抓資料的時機點錯了
04/16 19:21, 1F

04/16 19:22, , 2F
你現在這支程式在把UI生好之前就會先把連線發出去
04/16 19:22, 2F

04/16 19:25, , 3F
把[dataTask resume]擺到最後面試試看
04/16 19:25, 3F

04/16 19:32, , 4F
為什麼NSOperationQueue與dispatch_async一起使用?
04/16 19:32, 4F

04/16 19:33, , 5F
明明功能是一樣的東西
04/16 19:33, 5F

04/16 19:34, , 6F
而且不建議在AppDelegate寫太多程式在內部,就跟不該將程
04/16 19:34, 6F

04/16 19:35, , 7F
程式只寫在一個main.h中一樣
04/16 19:35, 7F

04/16 20:46, , 8F
https://gist.github.com/ gist是好東西 請愛用...
04/16 20:46, 8F

04/16 21:50, , 9F
謝謝樓上,我把程式碼貼過去了
04/16 21:50, 9F

04/16 21:51, , 10F
我嘗試貼網址,但被PTT擋了
04/16 21:51, 10F

04/16 21:52, , 11F
另外我有把[dataTask resume]擺到最後面,但沒有用
04/16 21:52, 11F

04/16 21:58, , 12F
不要用0rz的縮址,用goo.gl
04/16 21:58, 12F

04/16 21:59, , 13F
網路功能放入ViewController裡面處理,這樣比較好控管
04/16 21:59, 13F

04/16 22:00, , 14F
Timing的問題
04/16 22:00, 14F

04/16 22:00, , 15F
04/16 22:00, 15F

04/16 22:01, , 16F
另外我現在把APP build在手機上似乎就都可以了...
04/16 22:01, 16F

04/16 22:01, , 17F
之前都是用模擬器開APP看
04/16 22:01, 17F

04/16 22:06, , 18F
但一樣是有時有,有時沒有...好詭異
04/16 22:06, 18F

04/16 23:38, , 19F
Race condition, 如果completionHandler 執行的時候
04/16 23:38, 19F

04/16 23:38, , 20F
你有在plist把非加密網址的連線權限開啟?
04/16 23:38, 20F

04/16 23:39, , 21F
NewsTableViewController 還不存在的話就會有問題
04/16 23:39, 21F

04/16 23:42, , 22F
在reloadData那行前加一行Log 就可以看出來
04/16 23:42, 22F

04/17 00:33, , 23F
有把plist 非加密連線權限開啟
04/17 00:33, 23F
文章代碼(AID): #1N4Xy0c8 (MacDev)
文章代碼(AID): #1N4Xy0c8 (MacDev)