user2252092
user2252092

Reputation: 61

Returning a bool value using dispatch_async

simple question this time, i've made a "utility" class in my new app which help me to call method in my different UIVIewController.

i use class function to that. But i'm facing a new problem, i have to check if icloud is available or not and i use :

+ (BOOL)isIcloudAvailable {

    BOOL isBothIcloudenabled = NO; 

    BOOL isIcloudAvailable = NO;


    NSURL *ubiq = [[NSFileManager defaultManager]
                   URLForUbiquityContainerIdentifier:nil];
    if (ubiq) {
        isIcloudAvailable = YES; 
    } else {
        isIcloudAvailable = NO; 
    }

    NSUserDefaults *aUseriCloud = [NSUserDefaults standardUserDefaults];
    BOOL boolPref = [aUseriCloud boolForKey:@"icloudEnabled"];

    if (boolPref && isIcloudAvailable)
    {
        isBothIcloudenabled = YES;
    }
    else {
        isBothIcloudenabled = NO;
    }

    return isBothIcloudenabled;

}

and on my UIVIewController i use :

if ([utils isIcloudAvailable])
{...
}

URLForUbiquityContainerIdentifier:nil must do be called on the main thread, how can i do that ? I only know dispatch_async but it does not work on a return function.

Thanks

Upvotes: 0

Views: 2501

Answers (1)

Cthutu
Cthutu

Reputation: 8917

Your UIViewController should be running on the main thread so a dispatch should by unnecessary. If you're calling a UIViewController method from a different thread, I would recommend you run the invocation of that method using dispatch_async on the main thread, rather than doing it in the method itself. That is, all method calls in the UIViewController be done on the main thread.

Despite that, blocks can set values outside their scope:

bool isCloudAvailable = NO;
dispatch_sync(dispatch_get_main_queue(), ^{
    isCloudAvailable = [utils isIcloudAvailable];
});

This will block the thread until the main thread runs the block.

Upvotes: 2

Related Questions