Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating/showing a UIAlertView in a Block statement

I'm pretty new to blocks in objective C. I've read the docs and I have a pretty basic understanding of them.

Why won't this work? This is a framework callback for requesting Calendar access. It takes a block as an argument. All I want to do is allocate and show the UIAlertView in the block, but it will crash when it tries to show.

I hope this isn't a silly question... all the intro examples on the net using blocks just show trivial examples with counters.

//Request access
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

            if (granted == FALSE) {
                UIAlertView *myAlert = [[[UIAlertView alloc]initWithTitle:@"Calendar Access Denied"
                                                                          message:@"<InfoText>"
                                                                         delegate:nil
                                                                cancelButtonTitle:@"OK"
                                                                otherButtonTitles:nil] autorelease];
                [myAlert show];
            }
            else {
                [self addToCalendar];
            }
        }];
like image 456
Shaun Budhram Avatar asked Oct 03 '12 00:10

Shaun Budhram


1 Answers

have you tried?

if (granted == FALSE)
{
    dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *myAlert = [[[UIAlertView alloc]initWithTitle:@"Calendar Access Denied"
                                                         message:@ <InfoText>"
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil] autorelease];
       [myAlert show];
    });
}

this makes calls back in the main thread, useful for mixing blocks and UIKit

like image 152
Yuliani Noriega Avatar answered Oct 04 '22 03:10

Yuliani Noriega