swift中利用系统线程实现异步加载数据同步更新UI
swift中的使用案例样式
// Mark: -数据源更新
typealias AddDataBlock = () ->Void
var updataBlock:AddDataBlock?
func loadLiveData(){
let grpup = DispatchGroup()
grpup.enter()
// //首页直播接口请求
HomeLiveProvider.request(.liveList) { result in
if case let .success(response) = result {
//解析数据
let data = try? response.mapJSON()
let json = JSON(data!)
if let mappedObject = JSONDeserializer<HomeLiveModel>.deserializeFrom(json: json.description) { // 从字符串转换为对象实例
self.lives = mappedObject.data?.lives
self.categoryVoList = mappedObject.data?.categoryVoList
// self.collectionView.reloadData()
// 更新tableView数据
// self.updataBlock?()
grpup.leave()
}
}
}
grpup.enter()
// //首页直播滚动图接口请求
HomeLiveProvider.request(.liveBannerList) { result in
if case let .success(response) = result {
//解析数据
let data = try? response.mapJSON()
let json = JSON(data!)
if let mappedObject = JSONDeserializer<HomeLiveBanerModel>.deserializeFrom(json: json.description) { // 从字符串转换为对象实例
self.homeLiveBanerList = mappedObject.data
// let index: IndexPath = IndexPath.init(row: 0, section: 1)
// self.collectionView.reloadItems(at: [index])
// 更新tableView数据
// self.updataBlock?()
grpup.leave()
}
}
}
grpup.enter()
// //首页直播排行榜接口请求
HomeLiveProvider.request(.liveRankList) { result in
if case let .success(response) = result {
//解析数据
let data = try? response.mapJSON()
let json = JSON(data!)
if let mappedObject = JSONDeserializer<HomeLiveRankModel>.deserializeFrom(json: json.description) { // 从字符串转换为对象实例
self.multidimensionalRankVos = mappedObject.data?.multidimensionalRankVos
// let index: IndexPath = IndexPath.init(row: 0, section: 2)
// self.collectionView.reloadItems(at: [index])
// 更新tableView数据
// self.updataBlock?()
grpup.leave()
}
}
}
grpup.notify(queue: DispatchQueue.main) {
self.updataBlock?()
}
}
oc中的使用样式
//一个并发队列
dispatch_queue_t queueE = dispatch_queue_create("group.queue", DISPATCH_QUEUE_CONCURRENT);
//一个线程组
dispatch_group_t groupE = dispatch_group_create();
////1.请求轮播图的数据
dispatch_group_async(groupE, queueE, ^{
dispatch_group_enter(groupE);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self requestCarousel:^{
dispatch_group_leave(groupE);
}];
});
});
////2.请求新闻轮播图的数据
dispatch_group_async(groupE, queueE, ^{
dispatch_group_enter(groupE);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self requestNews:^{
dispatch_group_leave(groupE);
}];
});
});
////3.校园活动【师生展评】
dispatch_group_async(groupE, queueE, ^{
dispatch_group_enter(groupE);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//3.校园活动【师生展评】
[self requestCampAcitivetis:^{
dispatch_group_leave(groupE);
}];
});
});
////4.尾部视图
dispatch_group_async(groupE, queueE, ^{
dispatch_group_enter(groupE);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//3.尾部视图
[self requestClass:^{
dispatch_group_leave(groupE);
}];
});
});
dispatch_group_notify(groupE, dispatch_get_main_queue(), ^{
//staticWeakSelf 随情况而定
[staticWeakSelf setUpUI];
});
//1.请求轮播图的数据
- (void)requestCarousel:(void(^)(void))leveBlock{
[AFHTTPRequestManager reqesutSuccess{
if (leveBlock) {
leveBlock();
}
} requestFaliure{
if (leveBlock) {
leveBlock();
}
}
}