2 Replies
      Latest reply on Oct 4, 2019 9:02 AM by rtyagi
      rtyagi Level 1 Level 1 (0 points)

        Hi,

         

        I am using disptach_group to check all the preconditions before executing the final request.

        Here I am checking the location permissions whether the user has granted one of the permissions (WhenInUse, Always )

         

         

        @implementation ConditionEvaluator
        
        + (void)evaluateConditionsWithcompletionHandler:(void (^) (NSError *_Nullable))completion {
        
            dispatch_group_t conditionGroup = dispatch_group_create(); 
           
            dispatch_group_enter(conditionGroup);
             
            LocationCondition *loctionCondition = [[LocationCondition alloc] init;
        
          [[loctionCondition requestPermissionWithHandler:^(CLAuthorizationStatus status) {
        
           dispatch_group_leave(conditionGroup);
        
        }];
        
        // Condition 2
          dispatch_group_enter(conditionGroup);
        // async call
         [condition2 checkWithCompletion:^(){
             dispatch_group_leave(conditionGroup);
        }];
        
        
        // more here
        //
            dispatch_group_notify(conditionGroup, dispatch_get_global_queue(0, 0), ^{
                completion(failures);
            });
        
        }
        
        @end
        

         

        The problem is the requestPermissionWithHandler's block never executes and thread freeze as dispatch_group_leave never gets called.

         

        I tried to debug LocationCondition implementation there I found that requestAlwaysAuthorization never gets called and so, no delegate callbacks of locationManager.

         

        @implementation LocationCondition
        
        - (void)requestPermissionWithHandler:(PermissionHandler)handler {
            _permissionHandler = [handler copy];
        
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.locManager requestAlwaysAuthorization];
            }); 
        
        }
        
        #pragma mark - CLLocationManagerDelegate
        
        - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
           
            if (self.permissionHandler) {
                self.permissionHandler(status);
            }
        }
        
        
        - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
          
            if (self.permissionHandler) {
                self.permissionHandler(CLLocationManager.authorizationStatus);
            }
        }
        
        @end

         

         

        I also tried the following but the block on the main queue never executes.

         

         

        dispatch_group_t conditionGroup = dispatch_group_create();

         

          dispatch_group_enter(conditionGroup);

            dispatch_async(dispatch_get_main_queue(), ^{

                NSLog(@"Inside main queue");

           

                dispatch_group_leave(conditionGroup);

            });

         

        dispatch_group_notify(conditionGroup, dispatch_get_global_queue(0, 0), ^{

                completion(failures);

            });

         

        I don't know what I am doing wrong here.

         

        Any suggestions or help are welcome.

        • Re: dispatch_group never executes the code!
          eskimo Apple Staff Apple Staff (12,465 points)

          I tried to debug LocationCondition implementation there I found that requestAlwaysAuthorization never gets called and so, no delegate callbacks of locManager.

          So, with regards your second code snippet, that means that line 4 executes but line 7 doesn’t, right?

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: dispatch_group never executes the code!
              rtyagi Level 1 Level 1 (0 points)

              Thanks for writing back!

               

              In my second code snippet, line 4 & 7 never executes same like in the code snippet 1, handler of requestPermissionWithHandler method never get called.

               

              I finally found the issue, the issue was not in the implementation of ConditionEvaluator. It was in the thread calling below function.

              + (void)evaluateConditionsWithcompletionHandler:(void (^) (NSError *_Nullable))completion 

               

              In the calling thread, I had another dispatch_group_enter call to other dispatch group and waiting for that group to finish which was blocking my current thread to execute.

               

              I appreciate your prompt response!