Skip to content

Commit 867a449

Browse files
authored
Merge pull request #1417 from OneSignal/fix/delay_result_crash
Fix crash when handling a dialog result
2 parents 5626c7f + 790b776 commit 867a449

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignalDialogController.m

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ - (void)presentDialogWithTitle:(NSString * _Nonnull)title withMessage:(NSString
6060
//ensure this UI code executes on the main thread
6161
dispatch_async(dispatch_get_main_queue(), ^{
6262
let request = [[OSDialogRequest alloc] initWithTitle:title withMessage:message withActionTitles:actionTitles withCancelTitle:cancelTitle withCompletion:completion];
63-
64-
[self.queue addObject:request];
65-
66-
//check if already presenting a different dialog
67-
//if so, we shouldn't present on top of existing dialog
68-
if (self.queue.count > 1)
69-
return;
70-
63+
@synchronized (self.queue) {
64+
[self.queue addObject:request];
65+
66+
//check if already presenting a different dialog
67+
//if so, we shouldn't present on top of existing dialog
68+
if (self.queue.count > 1)
69+
return;
70+
}
7171
[self displayDialog:request];
7272
});
7373
}
@@ -101,23 +101,30 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto
101101

102102
- (void)delayResult:(int)result {
103103
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
104-
let currentDialog = self.queue.firstObject;
105-
106-
if (currentDialog.completion)
107-
currentDialog.completion(result);
108-
109-
[self.queue removeObjectAtIndex:0];
110-
111-
//check if no more dialogs left to display in queue
112-
if (self.queue.count == 0)
113-
return;
114-
115-
let nextDialog = self.queue.firstObject;
116-
117-
[self displayDialog:nextDialog];
104+
OSDialogRequest *nextDialog = nil;
105+
@synchronized (self.queue) {
106+
if (self.queue.count > 0) {
107+
let currentDialog = self.queue.firstObject;
108+
109+
if (currentDialog.completion)
110+
currentDialog.completion(result);
111+
112+
[self.queue removeObjectAtIndex:0];
113+
}
114+
115+
//check if no more dialogs left to display in queue
116+
if (self.queue.count == 0)
117+
return;
118+
119+
nextDialog = self.queue.firstObject;
120+
}
121+
if (nextDialog != nil) {
122+
[self displayDialog:nextDialog];
123+
}
118124
});
119125
}
120126

127+
// Unused. Currently only referenced in player model unit tests
121128
- (void)clearQueue {
122129
self.queue = [NSMutableArray new];
123130
}

0 commit comments

Comments
 (0)