Skip to content

Commit

Permalink
[MB-2531] Fix main thread warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rlepinski committed Jan 31, 2018
1 parent e447f4e commit 5f6996b
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions src/ios/UAirshipPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand All @@ -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"];
Expand Down Expand Up @@ -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);
}];
}
Expand Down Expand Up @@ -905,30 +899,23 @@ - (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);
}];
}

- (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);
}];
}
Expand All @@ -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);
}];
Expand Down

0 comments on commit 5f6996b

Please sign in to comment.