1. 程式人生 > >遇到線程阻塞,主線程死亡的問題,線程與信號量的使用

遇到線程阻塞,主線程死亡的問題,線程與信號量的使用

block ipa orm table rep option signal 阿裏雲 con

最近使用阿裏雲的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<HXQADVModel*>* tempAdvList = [[NSMutableArray alloc] init];

NSMutableArray<HXQQuestionModel*>* tempQuestionList = [[NSMutableArray alloc] init];

NSMutableArray<HXQFunActivityModel*>* tempActivityList = [[NSMutableArray alloc] init];

NSMutableArray<HXQStarWarsListModel*>* 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];

}];

}

});

}

遇到線程阻塞,主線程死亡的問題,線程與信號量的使用