Skip to content

Commit a1a66bf

Browse files
committed
Synchronize queue reads and writes on self.queue
Additionally verify that queue.count > 0 prior to removing object at index 0
1 parent 2fe6e89 commit a1a66bf

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignalDialogController.m

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,16 @@ - (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-
71-
[self displayDialog:request];
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+
71+
[self displayDialog:request];
72+
}
7273
});
7374
}
7475

@@ -101,23 +102,28 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto
101102

102103
- (void)delayResult:(int)result {
103104
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];
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+
let nextDialog = self.queue.firstObject;
120+
121+
[self displayDialog:nextDialog];
122+
}
118123
});
119124
}
120125

126+
// Unused. Currently only referenced in player model unit tests
121127
- (void)clearQueue {
122128
self.queue = [NSMutableArray new];
123129
}

0 commit comments

Comments
 (0)