- (void)requestCityDataWithBlock:(void(^)(NSArray *array))block
{
// 开启子线程
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString *str = @"http://api.XXX.com/v1/metadata/get_cities_with_deals?appkey=42960815&sign=6AFD15FED56B37F265210060C38B776D55EF8148";
self.cityArray = [NSMutableArray array];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
self.cityArray = dic[@"cities"];
// 回到主线程更新UI(把字符串传递过去)
dispatch_async(dispatch_get_main_queue(), ^{
block(self.cityArray);
});
}];
});
}
在controller中 实现单例中的 方法
[[CityManager shareManager] requestCityDataWithBlock:^(NSArray *array) {
self.dataArray = [NSMutableArray arrayWithArray:array];
[self.tableView reloadData];
}];