Search code examples
iphoneobjective-ciosgrand-central-dispatchcore-telephony

Unhiding a view is very slow in CTCallCenter callEventHandler


Reposting with more concise and focused question after original question went unanswered. Also adding more insight into the problem after another day of research:

In my app delegate (didFinishLaunching), I set up a callEventHandler on CTCallCenter. The idea is that when a callState changes, I post a notification with a userInfo dict containing the call.callState. In my view, I observe this notification, and when the userInfo dict contains a value of CTCallDisconnected, I want to unhide a view.

The problem I'm having is that the unhiding aspect is taking, almost consistenly, ~ 7 seconds. Everything else is working fine, and I know this because I NSLog before and after the unhiding, and those logs appear immediately, but the darned view still lags for 7 seconds.

Here's my code:

appDidFinishLaunching:

self.callCenter = [[CTCallCenter alloc] init];
    self.callCenter.callEventHandler = ^(CTCall* call) {
        // anounce that we've had a state change in our call center
        NSDictionary *dict = [NSDictionary dictionaryWithObject:call.callState forKey:@"callState"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CTCallStateDidChange" object:self userInfo:dict];
    };

I then listen for this notification when a user taps a button that dials a phone number:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ctCallStateDidChange:) name:@"CTCallStateDidChange" object:nil];

Then, in ctCallStateDidChange:

- (void)ctCallStateDidChange:(NSNotification *)notification
{
   NSLog(@"121");
   NSString *callInfo = [[notification userInfo] objectForKey:@"callState"];
   if ([callInfo isEqualToString:CTCallStateDisconnected]) {
      NSLog(@"before show");
      [self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
      NSLog(@"after show");
   }
}

I've tracked the problem down to the if condition in the above code sample:

 if ([[userInfo valueForKey:@"userInfo"] valueForKey:@"callState"] == CTCallStateDisconnected) {

If I simply replace that with:

if (1 == 1) {

Then the view appears immediately!

The thing is, those NSLog statements are logging immediately, but the view is lagging in it's unhiding. How could that condition cause only part of it's block to execute immediately, and the rest to wait ~ 7 seconds?

Thanks!


Solution

  • Try changing your code to this:

    - (void)ctCallStateDidChange:(NSNotification *)notification
    {
       NSLog(@"121");
       NSString *callInfo = [[notification userInfo] objectForKey:@"callState"];
       if ([callInfo isEqualToString:CTCallStateDisconnected]) {
          NSLog(@"before show");
          [self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
          NSLog(@"after show");
       }
    }
    

    Note:

    • The parameter is an NSNotification, not an NSDictionary
    • I would not compare strings with ==
    • No need to cast the view to change the hidden property
    • Use NO instead of false

    Update: Got an idea: Could you try the following, please, in between the NSLogs?

    dispatch_async(dispatch_get_main_queue(), ^{
       [self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
    });
    

    Reading the CTCallCenter doc, it seems the callEventHandler is dispatched on "the default priority global dispatch queue", which is not the main queue where all the UI stuff happens.