I have multiple nsnotificationcenters running at one time that display an alert when triggered. At times, this causes more than one alert to fire at a time but of course you can only display one and the other does not appear. What is the best way handle this situation so multiple alerts can go in succession.
I have tired having the alerts in one method and when one alert is showing, put another notification in an array and then run through that array but that is not working correctly. I have also tried to have the alerts in separate methods but that is not working either.
I have looked at using semaphores but could not find a good example.
This is my notifications which works as expected. I was looking for some advice on the notifications also. Where would be the best place to add the observer, in the viewDidAppear of viewDidLoad. viewDidLoad give a warning whenever an alert is displayed because it wants to display on a view that is not in the hierarchy.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// * 26 APR 2019 * 1.0.4.0
// Add observer for notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROC" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROP" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"CARGO" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"PAX" object:nil];
}
This is my selector method using a single method for all of the alerts. I am new to coding so I am sure this is not good practice so any advice would be appreciated. I am trying to put any additional notifications in an array if the current view is a uialertcontroller and then run through the array and display those alerts after but that is not working how i would like it to.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// * 26 APR 2019 * 1.0.4.0
// Add observer for notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROC" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"ROP" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"CARGO" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"PAX" object:nil];
}
- (void)receivedNotification:(NSNotification *)notification {
NSMutableDictionary *msgData = [[FlightDataInput sharedFlightDataInput] dataForPage:4];
NSMutableArray *alertArray = [[NSMutableArray alloc] init];
if([self.presentedViewController isKindOfClass:[UIAlertController class]]) {
[alertArray addObject:notification];
}
if(![self.presentedViewController isKindOfClass:[UIAlertController class]] && [alertArray count] == 0) {
if([notification.name isEqualToString: @"ROC"]) {
UIAlertController *alertRoc = [UIAlertController alertControllerWithTitle:[msgData valueForKey:@"rocTitle"] message:[msgData valueForKey:@"rocMsg"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alertRoc dismissViewControllerAnimated:YES completion:nil];
}];
[alertRoc addAction:ok];
[self presentViewController:alertRoc animated:NO completion:nil];
}
if ([notification.name isEqualToString:@"ROP"]) {
UIAlertController *alertRop = [UIAlertController alertControllerWithTitle:[msgData valueForKey:@"ropTitle"] message:[msgData valueForKey:@"ropMsg"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alertRop dismissViewControllerAnimated:YES completion:nil];
}];
[alertRop addAction:ok];
[self presentViewController:alertRop animated:NO completion:nil];
}
if ([notification.name isEqualToString:@"CARGO"]) {
UIAlertController *alertCargo = [UIAlertController alertControllerWithTitle:[msgData valueForKey:@"cargoTitle"] message:[msgData valueForKey:@"cargoMsg"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alertCargo dismissViewControllerAnimated:YES completion:nil];
}];
[alertCargo addAction:ok];
[self presentViewController:alertCargo animated:NO completion:nil];
}
if ([notification.name isEqualToString:@"PAX"]) {
UIAlertController *alertPax = [UIAlertController alertControllerWithTitle:[msgData valueForKey:@"paxTitle"] message:[msgData valueForKey:@"paxMsg"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alertPax dismissViewControllerAnimated:YES completion:nil];
}];
[alertPax addAction:ok];
[self presentViewController:alertPax animated:NO completion:nil];
}
}
if([alertArray count] > 0) {
for(int i = 0; i < [alertArray count]; i++) {
// creating the same alerts in here if there are alerts in the array
}
}
}
I have multiple nsnotificationcenters running at one time that display an alert when triggered. At times, this causes more than one alert to fire at a time but of course you can only display one and the other does not appear. What is the best way handle this situation so multiple alerts can go in succession.
I have tired having the alerts in one method and when one alert is showing, put another notification in an array and then run through that array but that is not working correctly. I have also tried to have the alerts in separate methods but that is not working either.