MBProcessHUD show/hide when using semaphore

82 views Asked by At

Hi I m using dispatch_group_t for async web call and during that wait period I want to show activity indicator. Here is my code which is not working for show/hide activity indicator.

- (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];   

        });
    });
}

I also tried with starting group in block but also not work.. Tried

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

also but no luck. Can anybody suggest me when i m doing wrong?? Or want else I can do to achieve this indicator show/hide issue.

0

There are 0 answers