0

For some reason, my app seems to take forever when I try to perform a segue. The mysterious loading time only appears, after I request permission for the AddressBook - if the Permission is already granted, there is no issue at all.

So, before actually using the Segue, i'm doing something like this:

Requesting Permission for Address Book

-(void)requestPermissionForContacts
{
  ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(nil, nil);
  if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
  {
    ABAddressBookRequestAccessWithCompletion(addressbook, ^(bool granted, CFErrorRef error) {
      if (granted)
      {
        NSLog(@"granted");
        [self didGrantPermissions];
      } else{
        NSLog(@"denied");
        [self didNotGrantPermission];
      }
    });
  }
  else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
  {
    [self didGrantPermissions];
  } else {
    [self didNotGrantPermission];
  }
  CFRelease(addressbook);
}

Whatever they user clicks:

-(void)didGrantPermissions
{
  [self performSegueWithIdentifier:@"addressBook" sender:nil];
}

-(void)didNotGrantPermission
{
  [[[UIAlertView alloc] initWithTitle:@"Address Book" message:@"To access this feature, please allow us to access your Address Book" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
}

And after that,

prepareForSegue

 if ([segue.identifier isEqualToString:@"addressBook"])
  {
    CWAddressBookViewController *abvc = segue.destinationViewController;
    abvc.delegate = self;
    abvc.addressType = self.addressType;
  } 

If it is the first time, the App is accessing this view (so, if its still asking for permission) it takes quite a while (20sec!) until -(void)viewDidLoad is being called on CWAddressBookViewControlller

If the permission is already granted, it is performing the segue as expected in the blink of an eye.

Any idea what im doing wrong here?

Martin Lang
  • 355
  • 1
  • 3
  • 11
  • So where exactly are them 20seconds spent? On the requestPermissionForContacts or performSegueWIthIndentifier ? – Yohst Jan 08 '14 at 04:31
  • Neither. the ~20s are being lost after performSegueWithIdentifier is done but before viewDidLoad is called. – Martin Lang Jan 08 '14 at 04:59
  • So what are you creating/loading in CWAddressBookViewController? Could it be you are doing some heavy lifting there? The issue may not have anything to do with getting permission but more with what are you loading first time and what is in memory already allocated/initialized second time... – Yohst Jan 08 '14 at 05:04
  • The issue is - if I already have permission, the load is being done right away. So the time gap actually occurs before i do any data-fetching. – Martin Lang Jan 08 '14 at 05:08
  • hmm, poking through the Apple docs a bit, I see that the completion handler for ABAddressBookRequestAccessWithCompletion is called on an arbitrary queue. You should make sure that your GUI code is called on the main thread. So wrap your calls to your segue in a (dispatch_get_main_queue(), ^{ [self didGrantPermissions] }); – Yohst Jan 08 '14 at 05:26
  • Yes, I was suspecting that as well and tried that - Sadly it had the same result there. Well, think i have to go back on pushing all the functions in the main queue. – Martin Lang Jan 08 '14 at 07:40
  • For whatever its worth, I built a quick test app with the exact code you provided above. It ran without delays on simulator as well as iPhone.... – Yohst Jan 08 '14 at 23:59
  • Yeah, I was not able to figure out the issue either. I ended up asking for permissions after the segue, with seemed to remove this problem. Still weird tough. – Martin Lang Jan 09 '14 at 07:23

0 Answers0