IOS中如何将开源库AFNetworking的GET函数中获取的数据,在函数体外利用呢?

代码如下,如何在GET函数外获取到在其内解析到的数据,从而加以利用呢?

 //
//  ViewController.m
//  1
//
//  Created by   on 16-2-2.
//  Copyright (c) 2016年  . All rights reserved.
//

#import "ViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "XiaHuaModel.h"
#import "XiaoHuaCell.h"


@interface ViewController ()
{
   __block NSMutableArray *arrData;
    UITableView *xiaoHuaView;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *url = @"http://localhost/json/2.json";

    xiaoHuaView = [[UITableView alloc]initWithFrame:self.view.bounds];
    xiaoHuaView.delegate = self;
    xiaoHuaView.dataSource = self;
    [self.view addSubview:xiaoHuaView];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSError *err= nil;
        //XiaHuaModel用于解析JSON数据为对象,它继承自第三方开源库JSONModel
        XiaHuaModel *xiaHua =  [[XiaHuaModel alloc]initWithDictionary:(NSDictionary *)responseObject error:&err];
        NSArray *detailArray = xiaHua.detail;
        arrData = [NSMutableArray arrayWithArray:detailArray];
        // detailModel *detail = [detailArray objectAtIndex:indexPath.row];
        // NSDictionary *detailDictionary = [detail toDictionary];
        NSLog(@"AFNetworking GET 函数内部数据为: %d",arrData.count);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error...%@",error);
    }];

     NSLog(@"AFNetworking GET 函数外部数据为: %d",arrData.count);

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection函数中的数据为 : %d",arrData.count);
        //此处期望获得值为2,但是却得到0
    return arrData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"XiaoHuaCell";
    XiaoHuaCell *cell = (XiaoHuaCell *)[tableView dequeueReusableCellWithIdentifier:Identifier];
    if (!cell) {
        cell = [[XiaoHuaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }

    return cell;

}

@end

**执行的结果为(执行的顺序与返回值不知是如何得到的,请指教)

2016-02-06 05:35:36.347 1[1945:60b] AFNetworking GET 函数外部数据为: 0
2016-02-06 05:35:36.365 1[1945:60b] numberOfRowsInSection函数中的数据为 : 0
2016-02-06 05:35:36.481 1[1945:60b] AFNetworking GET 函数内部数据为: 4
**

该段代码中 http://localhost/json/2.json 中的数据如下:

  {
  "status": "000000",
  "desc": null,
  "detail": [
    {
      "id": 40052,
      "xhid": 40052,
      "author": "不要抓我",
      "content": "有多少人,当年在学校为了夜里出去上网锯窗户过?有多少人翻墙的时候后跳到臭水沟了?",
      "picUrl": "",
      "status": "1"
    },
    {
      "id": 40051,
      "xhid": 40051,
      "author": "咸鱼翻跟头",
      "content": "一个人在外面打工,手伤了缝了5针也不敢跟家人说,怕她们担心,一个人硬撑着,可心里怎么那么难过啊!",
      "picUrl": "http://img.appd.lengxiaohua.cn/2016/01/31/bd82974300ced_o.jpg",
      "status": "1"
    }
  ]
}
     [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSError *err= nil;
        //XiaHuaModel用于解析JSON数据为对象,它继承自第三方开源库JSONModel
        XiaHuaModel *xiaHua =  [[XiaHuaModel alloc]initWithDictionary:(NSDictionary *)responseObject error:&err];
        NSArray *detailArray = xiaHua.detail;
        arrData = [NSMutableArray arrayWithArray:detailArray];
        // detailModel *detail = [detailArray objectAtIndex:indexPath.row];
        // NSDictionary *detailDictionary = [detail toDictionary];
        NSLog(@"AFNetworking GET 函数内部数据为: %d",arrData.count);
                //注意这一句
                [xiaoHuaView reloaddata];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error...%@",error);
    }];

     NSLog(@"AFNetworking GET 函数外部数据为: %d",arrData.count);

其实也就是在你Get到数据之后,调用一下tableview的刷新,tableview就会重新加载数据,那么你新的数据就能被tableview展示出来了

问题的关键是get是异步的,你需要在主程序等待它执行完,你主程序可以做循环,get函数将结果放入全局变量,主程序获取。

你都知道block了 就要理解异步编程的思想 你可以再调用前定义变量 这样block中可以捕获这个变量 从而把数据从get中返回数据给变量

如何解决结果由block返回情况下的同步问题
记录一下