@@ -469,13 +469,6 @@ + (void)init {
469469 static _Atomic (BOOL ) gProtectedDataAvailable = NO ;
470470 static dispatch_once_t protectedDataOnce;
471471 dispatch_once (&protectedDataOnce, ^{
472- // Seed the cache on the main thread once. UIApplication APIs are main-thread-only.
473- // On a normal launch this resolves to YES near-immediately; on a prewarm launch
474- // before first unlock it stays NO and the notification will flip it later.
475- dispatch_async (dispatch_get_main_queue (), ^{
476- atomic_store (&gProtectedDataAvailable , UIApplication.sharedApplication .isProtectedDataAvailable );
477- });
478-
479472 NSNotificationCenter *center = NSNotificationCenter .defaultCenter ;
480473 [center addObserverForName: UIApplicationProtectedDataDidBecomeAvailable
481474 object: nil
@@ -497,6 +490,25 @@ + (void)init {
497490 OneSignalConfig.isProtectedDataAvailableProvider = ^BOOL {
498491 return atomic_load (&gProtectedDataAvailable );
499492 };
493+
494+ // Seed the cached flag. UIApplication APIs are main-thread-only. `initialize` is normally
495+ // called from `application:didFinishLaunchingWithOptions:` on the main thread, so seed
496+ // synchronously here — that way the flag is correct before `startUserManager` runs later
497+ // in this same `init`. If we're off the main thread, hop to main to read it; once it
498+ // resolves to available, re-drive `start()`, because the synchronous `start()` below will
499+ // have been gated out and a normal (already-unlocked) launch never posts
500+ // UIApplicationProtectedDataDidBecomeAvailable to re-drive it.
501+ if ([NSThread isMainThread ]) {
502+ atomic_store (&gProtectedDataAvailable , UIApplication.sharedApplication .isProtectedDataAvailable );
503+ } else {
504+ dispatch_async (dispatch_get_main_queue (), ^{
505+ BOOL available = UIApplication.sharedApplication .isProtectedDataAvailable ;
506+ atomic_store (&gProtectedDataAvailable , available);
507+ if (available) {
508+ [OneSignalUserManagerImpl.sharedInstance start ];
509+ }
510+ });
511+ }
500512 });
501513
502514 // TODO: We moved this check to the top of this method, we should test this.
0 commit comments