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.