# SDL: Make sure the UIKit message box is being handled on the main thread

**URL:** <https://discourse.libsdl.org/t/sdl-make-sure-the-uikit-message-box-is-being-handled-on-the-main-thread/35292>\
**Category:** SDL Commits\
**Created:** [April 7, 2022, 6:34pm UTC](https://discourse.libsdl.org/t/sdl-make-sure-the-uikit-message-box-is-being-handled-on-the-main-thread/35292 "2022-04-07T18:34:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [April 7, 2022, 6:34pm UTC](https://discourse.libsdl.org/t/sdl-make-sure-the-uikit-message-box-is-being-handled-on-the-main-thread/35292/1 "2022-04-07T18:34:02Z")

</div>

> <https://github.com/libsdl-org/SDL/commit/5613a5603245f600b36fe327c11c67f0e1ef2cc3>
>
> Potentially fixes https://github.com/libsdl-org/SDL/issues/4865

```diff
From 5613a5603245f600b36fe327c11c67f0e1ef2cc3 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 7 Apr 2022 11:32:22 -0700
Subject: [PATCH] Make sure the UIKit message box is being handled on the main
 thread

Potentially fixes https://github.com/libsdl-org/SDL/issues/4865
---
 src/video/uikit/SDL_uikitmessagebox.m | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/video/uikit/SDL_uikitmessagebox.m b/src/video/uikit/SDL_uikitmessagebox.m
index fdc20c08649..b6d56f35050 100644
--- a/src/video/uikit/SDL_uikitmessagebox.m
+++ b/src/video/uikit/SDL_uikitmessagebox.m
@@ -186,8 +186,8 @@ - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)
 #endif /* __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 */
 }
 
-int
-UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
+static void
+UIKit_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonid, int *returnValue)
 {
     BOOL success = NO;
 
@@ -199,12 +199,26 @@ - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)
     }
 
     if (!success) {
- return SDL_SetError("Could not show message box.");
+ *returnValue = SDL_SetError("Could not show message box.");
+ } else {
+ *returnValue = 0;
     }
-
- return 0;
 }
 
+int
+UIKit_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
+{ @autoreleasepool
+{
+ __block int returnValue = 0;
+
+ if ([NSThread isMainThread]) {
+ UIKit_ShowMessageBoxImpl(messageboxdata, buttonid, &returnValue);
+ } else {
+ dispatch_sync(dispatch_get_main_queue(), ^{ UIKit_ShowMessageBoxImpl(messageboxdata, buttonid, &returnValue); });
+ }
+ return returnValue;
+}}
+
 #endif /* SDL_VIDEO_DRIVER_UIKIT */
 
 /* vi: set ts=4 sw=4 expandtab: */

```
