Model.h
#importtypedef void(^callBackBlock)(NSDictionary * dic);
@interface LGModel : NSObject
//单例
+(instancetype)showData;
-(void)getDataAndBlock:(callBackBlock)block;
@end
Model.m
#import "LGModel.h"
#define URL @"http://116.211.167.106/api/live/aggregation?uid=133825214&interest=1"
static LGModel * m=nil;
@implementation LGModel
{
NSDictionary * dic;
}
+(instancetype)showData{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
m =[[LGModel alloc]init];
});
return m;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (m==nil) {
m =[super allocWithZone:zone];
}
return m;
}
-(id)copy{
return self;
}
-(id)mutableCopy{
return self;
}
-(void)getDataAndBlock:(callBackBlock)block{
//系统自带解析
NSURL * url =[NSURL URLWithString:URL];
NSURLSession * session =[NSURLSession sharedSession];
//数据任务
NSURLSessionTask * task =[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dic =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (block && dic) {
block(dic);
}
}];
[task resume];
}
@end
Viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor =[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1];
header = [MJRefreshHeaderView header];
header.scrollView = cellect;
header.delegate = self;
[header beginRefreshing];
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView{
if ([refreshView isKindOfClass:[MJRefreshHeaderView class]]) {
//2秒以后调用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[LGModel showData]getDataAndBlock:^(NSDictionary *mydic) {
dic = mydic;
//yymodel
m =[[LGZ alloc]init];
[m yy_modelSetWithDictionary:dic];
dispatch_async(dispatch_get_main_queue(), ^{
[table reloadData];
[header endRefreshing];
});
}];
});
}
}