zoukankan      html  css  js  c++  java
  • 遇到线程阻塞,主线程死亡的问题,线程与信号量的使用

    最近使用阿里云的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];

                }];

            }

        });

    }

  • 相关阅读:
    java 生成二维码
    Web Service测试工具小汇
    ADF控件ID变化引发JS无法定位控件的解决方法
    create database link
    Jdeveloper下Svn的使用
    TortoiseSVN菜单项功能说明
    JSFF或JSF页面加载时触发JavaScript之方法
    更改ORACLE归档路径及归档模式
    更改oracle归档模式路径
    将Oracle数据库设置为归档模式及非归档模式
  • 原文地址:https://www.cnblogs.com/tangyuanby2/p/8358944.html
Copyright © 2011-2022 走看看