MacDev 板


LINE

大家好,小弟最近想自己试着使用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://webptt.com/cn.aspx?n=bbs/MacDev/M.1460805376.A.988.html
1F:→ Esvent: reload的时机点没错 但抓资料的时机点错了 04/16 19:21
2F:→ Esvent: 你现在这支程式在把UI生好之前就会先把连线发出去 04/16 19:22
3F:→ Esvent: 把[dataTask resume]摆到最後面试试看 04/16 19:25
4F:→ darktt: 为什麽NSOperationQueue与dispatch_async一起使用? 04/16 19:32
5F:→ darktt: 明明功能是一样的东西 04/16 19:33
6F:→ darktt: 而且不建议在AppDelegate写太多程式在内部,就跟不该将程 04/16 19:34
7F:→ darktt: 程式只写在一个main.h中一样 04/16 19:35
8F:推 neotek: https://gist.github.com/ gist是好东西 请爱用... 04/16 20:46
9F:→ strife00: 谢谢楼上,我把程式码贴过去了 04/16 21:50
10F:→ strife00: 我尝试贴网址,但被PTT挡了 04/16 21:51
11F:→ strife00: 另外我有把[dataTask resume]摆到最後面,但没有用 04/16 21:52
12F:→ darktt: 不要用0rz的缩址,用goo.gl 04/16 21:58
13F:→ darktt: 网路功能放入ViewController里面处理,这样比较好控管 04/16 21:59
14F:→ darktt: Timing的问题 04/16 22:00
15F:→ strife00: 好了,https://goo.gl/SF6ZkL 04/16 22:00
16F:→ strife00: 另外我现在把APP build在手机上似乎就都可以了... 04/16 22:01
17F:→ strife00: 之前都是用模拟器开APP看 04/16 22:01
18F:→ strife00: 但一样是有时有,有时没有...好诡异 04/16 22:06
19F:推 johnlinvc: Race condition, 如果completionHandler 执行的时候 04/16 23:38
20F:→ powerwolf543: 你有在plist把非加密网址的连线权限开启? 04/16 23:38
21F:→ johnlinvc: NewsTableViewController 还不存在的话就会有问题 04/16 23:39
22F:推 johnlinvc: 在reloadData那行前加一行Log 就可以看出来 04/16 23:42
23F:→ strife00: 有把plist 非加密连线权限开启 04/17 00:33







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP