国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發-AFNetworking/NSURLSession異步處理

2019-11-07 23:47:15
字體:
來源:轉載
供稿:網友

iOS開發-AFNetworking/NSURLsession異步處理

相信對于GitHub上著名的AFNetworking API,一定都不陌生。本文是一篇基礎使用的心得,老手請繞行。

AFNetworking與NSURLSession都是依靠block內的代碼進行異步處理來得到網絡數據(比如by jSON格式),這樣做的好處就是不會阻塞主隊列的進程,當block內的代碼執行完畢時,進行回調處理,回到主隊列。這樣當需要得到很多網絡數據時,不會讓用戶在一個空白的頁面等待很久,可以把不需要網絡數據的部分提前加載,增加用戶體驗。而且現在NSURLConnection這種在主隊列操作的方法已經dePRecated了,盡量使用這兩種異步處理的方式。

以下為AFNetworking的示例代碼(使用jSON格式,源為iTunes rss top 10 songs)

復制代碼
 1 #import "FetchData.h" 2 #import <AFNetworking/AFNetworking.h> 3  4 @implementation FetchData 5  6 - (void)fetchData 7 { 8     self.resultArray = [[NSMutableArray alloc]init]; 9     AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];10     [manager GET:@"https://itunes.apple.com/us/rss/topsongs/limit=10/json" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, NSDictionary *responSEObject) {11         NSDictionary *feed = [responseObject objectForKey:@"feed"];12         NSArray *entry = [feed objectForKey:@"entry"];13         for (NSDictionary *detailDict in entry) {14             [self.resultArray addObject:[detailDict valueForKeyPath:@"title.label"]];15         }16         [[NSNotificationCenter defaultCenter]postNotificationName:@"reload" object:self];17         18         19     } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {20         NSLog(@"%@", [error description]);21     }];22 }復制代碼

關于import AFNetworking API ,鏈接https://github.com/AFNetworking/AFNetworking

復制代碼
 1 - (void)fetchSessionData 2 { 3     self.resultArray = [[NSMutableArray alloc]init]; 4     NSURLSession *session = [NSURLSession sharedSession]; 5     NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/us/rss/topsongs/limit=10/json"]; 6     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 7     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 8         NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 9         NSDictionary *feed = [json objectForKey:@"feed"];10         NSArray *entry = [feed objectForKey:@"entry"];11         for (NSDictionary *detailDict in entry) {12             [self.resultArray addObject:[detailDict valueForKeyPath:@"title.label"]];13             14         }15         [[NSNotificationCenter defaultCenter]postNotificationName:@"reload" object:self];16     }];17     [dataTask resume];18 }復制代碼

 

[[NSNotificationCenter defaultCenter]postNotificationName:@"reload" object:self];

是關鍵,因為是異步處理,所以并不知道block內的內容會什么時候完成,由于示例在MVC結構下,所以使用[NSNotificationCenter]來監控block執行情況,如果方法都在一個ViewController中,則將該代碼換成[self.tableView reloadData];

這里說明一下原因,因為UIKit控件都是依據所在的viewController存在的,即使屬性改為strong,依然會在此viewcontroller消失后也會release,所以如果是MVC結構,在Model文件中,即使初始化了一個viewController的實例,并使其tableView reloadData,重新返回這個viewController時,還是會再初始化這個tableView所以是無效的。

之后是viewController代碼

復制代碼
 1 @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> 2 @property (nonatomic)UITableView *tableView; 3 @property (nonatomic)FetchData *fetchData; 4 @end 5  6 @implementation ViewController 7  8 - (void)viewDidLoad { 9     [super viewDidLoad];10     self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];11     [self.tableView setDelegate:self];12     [self.tableView setDataSource:self];13     [self.view addSubview:self.tableView];14     self.fetchData = [[FetchData alloc]init];15     [self.fetchData fetchData];16     17     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reload) name:@"reload" object:nil];18 }19 20 - (void)reload{21     [self.tableView reloadData];22     [[NSNotificationCenter defaultCenter]removeObserver:self name:@"reload" object:nil];23 }24 25 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView26 {27     return 1;28 }29 30 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section31 {32     return self.fetchData.resultArray.count;33 }34 35 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath36 {37     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];38     if (!cell) {39         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];40     }41     cell.textLabel.text = [self.fetchData.resultArray objectAtIndex:indexPath.row];42     return cell;43 }復制代碼

注意17行,以及20行的reload方法,在調用fetchData方法之后,發出一個觀察者,當觀察到post出的notification時,則調用reload方法,刷新tableView,實現異步處理

以下是一個NSURLSession版在viewController里實現的方法,與AFNetworking大同小異

復制代碼
 1 - (void)viewDidLoad { 2     [super viewDidLoad]; 3     self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds]; 4     [self.tableView setDelegate:self]; 5     [self.tableView setDataSource:self]; 6     [self.view addSubview:self.tableView]; 7     [self fetchSessionData]; 8 } 9 10 - (void)fetchSessionData11 {12     self.resultArrayLocal = [[NSMutableArray alloc]init];13     NSURLSession *session = [NSURLSession sharedSession];14     NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/us/rss/topsongs/limit=10/json"];15     NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {16         NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];17         NSDictionary *feed = [json objectForKey:@"feed"];18         NSArray *entry = [feed objectForKey:@"entry"];19         for (NSDictionary *detailDict in entry) {20             //[self.resultArrayLocal addObject:[detailDict valueForKeyPath:@"title.label"]];21             NSDictionary *title = [detailDict objectForKey:@"title"];22             NSString *str = [title objectForKey:@"label"];23             [self.resultArrayLocal addObject:str];24             25         }26         27         [self.tableView reloadData];28     }];29     [dataTask resume];30 }31 32 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView33 {34     return 1;35 }36 37 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section38 {39     return self.fetchData.resultArray.count;40 }41 42 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath43 {44     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];45     if (!cell) {46         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];47     }48     cell.textLabel.text = [self.fetchData.resultArray objectAtIndex:indexPath.row];49     return cell;50 }復制代碼

主要是想提一下MVC下[NSNotificationCenter]的用法,由此,異步刷新實現


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 禄丰县| 永登县| 定边县| 文水县| 宁城县| 巍山| 恩施市| 容城县| 巨鹿县| 青田县| 阜阳市| 科技| 习水县| 南安市| 莱西市| 临高县| 桐乡市| 嘉峪关市| 三原县| 奈曼旗| 蓬溪县| 大同县| 绥化市| 芒康县| 互助| 肇源县| 全椒县| 宁津县| 定安县| 邓州市| 沅江市| 荣成市| 洛宁县| 龙口市| 河北区| 江陵县| 舟曲县| 徐州市| 宁津县| 平舆县| 南郑县|