使用信号量时,MBProcessHUD显示/隐藏

时间:2016-12-18 16:50:26

标签: ios multithreading block semaphore mbprogresshud

您好我使用dispatch_group_t进行异步Web调用,在此等待期间我想显示活动指示符。 这是我的代码,它不适用于显示/隐藏活动指示器。

- (BaseResponse*)getResponse:(BaseRequest*)request{

    request.header = [[Header alloc]init];
    request.header.osType = 2;       

    NSString *jsonBody = [request toJSONString];

    NSURL *requestURL = @"myurl";

    __block BaseResponse *baseResponse;

    [self showHUDProcessView];  //here activity indicator should start animating but not working as per expected.

    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);

    [[COMHandler sharedCOMHandler] makeServiceCall:jsonBody requestURL_:requestURL completion:^(ComResponse *comResponse){

        [self hideHUDProcessView]; //hide  activity when receive response

        if (comResponse != nil) {
            baseResponse = comResponse;
        }else{             
           //show alert
        }         
        dispatch_group_leave(group);                       
    }];             
   dispatch_group_wait(group,  DISPATCH_TIME_FOREVER);
   return baseResponse;
}
- (void) showHUDProcessView {     
     [MBProgressHUD showHUDAddedTo:viewController.view animated:YES];       
}

- (void) hideHUDProcessView {
   dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
              dispatch_async(dispatch_get_main_queue(), ^{                
                [MBProgressHUD hideHUDForView:viewController.view animated:YES];   

        });
    });
}

我也试过在街区开始组但也没有工作.. 试过

dispatch_semaphore_t activity =  dispatch_semaphore_create(0); 
       //show indicator
       ^myblock {
          //hide indicator
          dispatch_semaphore_signal(activity);
        }
    dispatch_semaphore_wait(activity, DISPATCH_TIME_FOREVER);

但也没有运气。 当我做错的时候有人能建议我吗? 或者我想做其他事情来实现这个指标显示/隐藏问题。

0 个答案:

没有答案