From 5f6996b7c813e2e1d5dbf5292821289dc77128ab Mon Sep 17 00:00:00 2001 From: Ryan Lepinski Date: Tue, 30 Jan 2018 16:16:05 -0800 Subject: [PATCH] [MB-2531] Fix main thread warnings --- src/ios/UAirshipPlugin.m | 61 +++++++++++++++------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/src/ios/UAirshipPlugin.m b/src/ios/UAirshipPlugin.m index a7076ebc..19e33bfb 100644 --- a/src/ios/UAirshipPlugin.m +++ b/src/ios/UAirshipPlugin.m @@ -237,24 +237,22 @@ - (CDVPluginResult *)pluginResultForValue:(id)value status:(CDVCommandStatus)sta } /** - * Helper method to perform a cordova command asynchronously. + * Helper method to perform a cordova command. * * @param command The cordova command. * @param block The UACordovaExecutionBlock to execute. */ - (void)performCallbackWithCommand:(CDVInvokedUrlCommand *)command withBlock:(UACordovaExecutionBlock)block { - [self.commandDelegate runInBackground:^{ - UACordovaCompletionHandler completionHandler = ^(CDVCommandStatus status, id value) { - CDVPluginResult *result = [self pluginResultForValue:value status:status]; - [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; - }; + UACordovaCompletionHandler completionHandler = ^(CDVCommandStatus status, id value) { + CDVPluginResult *result = [self pluginResultForValue:value status:status]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + }; - if (!block) { - completionHandler(CDVCommandStatus_OK, nil); - } else { - block(command.arguments, completionHandler); - } - }]; + if (!block) { + completionHandler(CDVCommandStatus_OK, nil); + } else { + block(command.arguments, completionHandler); + } } #pragma mark Phonegap bridge @@ -736,11 +734,11 @@ - (void)notificationAuthorizedOptionsDidChange:(UANotificationOptions)options { optedIn = alertBool || badgeBool || soundBool; NSDictionary *eventBody = @{ @"optIn": @(optedIn), - @"notificationOptions" : @{ - NotificationPresentationAlertKey : @(alertBool), - NotificationPresentationBadgeKey : @(badgeBool), - NotificationPresentationSoundKey : @(soundBool) } - }; + @"notificationOptions" : @{ + NotificationPresentationAlertKey : @(alertBool), + NotificationPresentationBadgeKey : @(badgeBool), + NotificationPresentationSoundKey : @(soundBool) } + }; UA_LINFO(@"Opt in status changed."); [self notifyListener:EventNotificationOptInStatus data:eventBody]; @@ -759,7 +757,7 @@ - (void)receivedNotificationResponse:(UANotificationResponse *)notificationRespo [event setValue:@(YES) forKey:@"isForeground"]; } else { UANotificationAction *notificationAction = [self notificationActionForCategory:notificationResponse.notificationContent.categoryIdentifier - actionIdentifier:notificationResponse.actionIdentifier]; + actionIdentifier:notificationResponse.actionIdentifier]; BOOL isForeground = notificationAction.options & UNNotificationActionOptionForeground; [event setValue:@(isForeground) forKey:@"isForeground"]; @@ -801,18 +799,14 @@ - (void)showInbox { - (void)displayMessageCenter:(CDVInvokedUrlCommand *)command { [self performCallbackWithCommand:command withBlock:^(NSArray *args, UACordovaCompletionHandler completionHandler) { - dispatch_async(dispatch_get_main_queue(), ^{ - [[UAirship defaultMessageCenter] display]; - }); + [[UAirship defaultMessageCenter] display]; completionHandler(CDVCommandStatus_OK, nil); }]; } - (void)dismissMessageCenter:(CDVInvokedUrlCommand *)command { [self performCallbackWithCommand:command withBlock:^(NSArray *args, UACordovaCompletionHandler completionHandler) { - dispatch_async(dispatch_get_main_queue(), ^{ - [[UAirship defaultMessageCenter] dismiss]; - }); + [[UAirship defaultMessageCenter] dismiss]; completionHandler(CDVCommandStatus_OK, nil); }]; } @@ -905,9 +899,7 @@ - (void)displayInboxMessage:(CDVInvokedUrlCommand *)command { // Store a weak reference to the MessageViewController so we can dismiss it later self.messageViewController = mvc; - dispatch_async(dispatch_get_main_queue(), ^{ - [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:navController animated:YES completion:nil]; - }); + [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:navController animated:YES completion:nil]; completionHandler(CDVCommandStatus_OK, nil); }]; @@ -915,20 +907,15 @@ - (void)displayInboxMessage:(CDVInvokedUrlCommand *)command { - (void)dismissInboxMessage:(CDVInvokedUrlCommand *)command { [self performCallbackWithCommand:command withBlock:^(NSArray *args, UACordovaCompletionHandler completionHandler) { - dispatch_async(dispatch_get_main_queue(), ^{ - [self.messageViewController dismissViewControllerAnimated:YES completion:nil]; - self.messageViewController = nil; - }); - + [self.messageViewController dismissViewControllerAnimated:YES completion:nil]; + self.messageViewController = nil; completionHandler(CDVCommandStatus_OK, nil); }]; } - (void)dismissOverlayInboxMessage:(CDVInvokedUrlCommand *)command { [self performCallbackWithCommand:command withBlock:^(NSArray *args, UACordovaCompletionHandler completionHandler) { - dispatch_async(dispatch_get_main_queue(), ^{ - [UALandingPageOverlayController closeAll:YES]; - }); + [UALandingPageOverlayController closeAll:YES]; completionHandler(CDVCommandStatus_OK, nil); }]; } @@ -944,9 +931,7 @@ - (void)overlayInboxMessage:(CDVInvokedUrlCommand *)command { return; } - dispatch_async(dispatch_get_main_queue(), ^{ - [UALandingPageOverlayController showMessage:message]; - }); + [UALandingPageOverlayController showMessage:message]; completionHandler(CDVCommandStatus_OK, nil); }];