主线程 子线程死掉_遇到线程阻塞,主线程死亡的问题,线程与信号量的使用...

最近使用阿里云的oss 上传文件遇到的问题

解决方式 外层加了队列,本文主要写线程和信号量  持续更新

dispatch_async(dispatch_get_global_queue(0, 0), ^{

// 分块上传

[upload multipartUpload:videoPath objectKey:objectKeyName];

if([[self viewController] respondsToSelector:@selector(creatBackViewForUpdatePress)]){

[[self viewController] performSelector:@selector(creatBackViewForUpdatePress) withObject:nil];

}

});

信号量 网络请求的顺序 :

先上代码

-(void)loadData{

WS(wSelf)

if (!ISNETWORK) {

[self showBlank:HXQBlankNoNetwork BlankBlock:^(HXQBlankType blankType) {

[wSelf loadData];

}];

[self.tableView.mj_header endRefreshing];

return;

}

dispatch_group_t group = dispatch_group_create();

NSMutableArray* tempAdvList = [[NSMutableArray alloc] init];

NSMutableArray* tempQuestionList = [[NSMutableArray alloc] init];

NSMutableArray* tempActivityList = [[NSMutableArray alloc] init];

NSMutableArray* tempStarWarsList = [[NSMutableArray alloc] init];

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

// 顶部广告

[HttpRequestUtil GET:Action_Home_Adv parameters:@{@"spaceId":@"4"} finished:^(AjaxResult *result) {

if (result.code == AjaxResultStateSuccess) {

NSArray* advArray = result.datas;

for (NSDictionary* diction in advArray) {

HXQADVModel* advModel = [HXQADVModel mj_objectWithKeyValues:diction];

[tempAdvList addObject:advModel];

}

}

dispatch_semaphore_signal(sema);

}];

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

});

if ([self isLogin]) {

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

[HttpRequestUtil GET:Action_NewQuestion_UserInfo parameters:@{@"userId":[UserModel getCurrentUserFromLocal].userId} finished:^(AjaxResult *result) {

if (result.code == AjaxResultStateSuccess) {

DLog(@"%@",result.datas);

self.userInfoModel = [HXQNQUserInfoModel mj_objectWithKeyValues:result.datas];

}

dispatch_semaphore_signal(sema);

}];

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

});

}

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

// 答题

[HttpRequestUtil GET:Answer_Submit_GetQuestionRepositorys parameters:@{@"count":@(20)} finished:^(AjaxResult *result) {

if (result.code == AjaxResultStateSuccess) {

NSArray* questionArray = result.datas;

for (NSDictionary* diction in questionArray) {

HXQQuestionModel* advModel = [HXQQuestionModel mj_objectWithKeyValues:diction];

[tempQuestionList addObject:advModel];

}

}

dispatch_semaphore_signal(sema);

}];

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

});

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

// 投票

[HttpRequestUtil GET:Answer_Submit_activity parameters:@{@"page":@"0",@"count":@"100",@"type":@"0"} finished:^(AjaxResult *result) {

if (result.code == AjaxResultStateSuccess) {

NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:result.originDatas options:NSJSONReadingMutableContainers error:nil];

NSArray* voteArray = result.datas;

for (NSDictionary* diction in voteArray) {

HXQFunActivityModel* model = [HXQFunActivityModel mj_objectWithKeyValues:diction];

model.handleTime = [(NSNumber*)[dic objectForKey:@"handleTime"] integerValue];

[tempActivityList addObject:model];

}

}

dispatch_semaphore_signal(sema);

}];

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

});

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

// 星战

[HttpRequestUtil GET:Action_Home_Info parameters:@{@"page":@"0",@"count":@"1"} finished:^(AjaxResult *result) {

if (result.code == AjaxResultStateSuccess) {

NSArray* starWarsArray = [result.datas objectForKey:@"list"];

for (NSDictionary* diction in starWarsArray) {

HXQStarWarsListModel* model = [HXQStarWarsListModel mj_objectWithKeyValues:diction];

[tempStarWarsList addObject:model];

}

}

dispatch_semaphore_signal(sema);

}];

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

});

dispatch_group_notify(group, dispatch_get_main_queue(), ^{

//刷新界面

[self.advList removeAllObjects];

[self.questionList removeAllObjects];

[self.activityList removeAllObjects];

[self.starWarsList removeAllObjects];

[self.dataList removeAllObjects];

[self.advList addObjectsFromArray:tempAdvList];

if (self.advList.count>0) {

NSMutableArray* advArray = [[NSMutableArray alloc] init];

for (HXQADVModel* model in self.advList) {

[advArray addObject:APP_URL(model.img)];

}

self.headerView.imageURLStringsGroup = advArray;

[self.tableView setTableHeaderView:self.headerView];

}else

{

UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];

[self.tableView setTableHeaderView:view];

}

[self.questionList addObjectsFromArray:tempQuestionList];

[self.activityList addObjectsFromArray:tempActivityList];

[self.starWarsList addObjectsFromArray:tempStarWarsList];

if (self.questionList.count>0) {

[self.dataList addObject:self.questionList];

}

if (self.starWarsList.count>0) {

[self.dataList addObject:self.starWarsList];

}

if (self.activityList.count>0) {

[self.dataList addObject:self.activityList];

}

[self.tableView.mj_header endRefreshing];

if (self.dataList.count>0) {

[self hidenBlank];

[self.tableView reloadData];

[self.tableView setTableFooterView:self.footerView];

}else

{

[self.tableView reloadData];

[self showBlank:HXQBlankNoDataType BlankBlock:^(HXQBlankType blankType) {

[wSelf loadData];

}];

}

});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值