0

I want to show user a alert in case my application stop before creating a window to show root view controller and i want to do this from a worker thread which will be running in a cpp file and will be calling swift file before a UIApplication.shared.delegate is created.So , I want to show a error message to user from worker thread without switching to main thread (without using DispactchQueue.main.sync{code...})

I tried a using a UIAlertController to present it from worker thread but xcode yelled at me to use it to call from main thread.Is there way to show error message to user to let him know to update/make changes in his system before start app??

.swift file

import UIKit

@objc class AlertPopUpInWin:NSObject{
    @objc func AlertPopUp(labelText:NSString){
        print(labelText)
        let delegate = UIApplication.shared.delegate
        let window = delegate?.window
        let vc = window?!.rootViewController as! ViewController
        let alert = UIAlertController(title: "hello", message: labelText as String, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))
        vc.present(alert, animated: true)
        
    }
}

.cpp file - to call AlertPopUP() function from cpp

#include "AlertPopUpDailog.hpp"
#include <iostream>
#include "SwiftWrapper.h"
#include "thread"
#include <cstdlib>
using namespace std;

void AlertPopUpDailog::UpdateAlertDailogInWorkerThread()
{
    std::thread t1(AlertPopUpDailog::SwiftFuncCaller);

    t1.detach();
    
}


void AlertPopUpDailog::SwiftFuncCaller()
{
    char* alertText = "Alert - hello";
    SwiftWrapper::AlertPopUpSwiftWrapper(alertText);
}

Abhishek
  • 1
  • 3
  • I don't follow your question. You say that you want to be able to show an alert before the root view controller is installed or the app delegate is created, but your swift code depends on the root view controller and app delegate. And yes, you must perform UI operations on the main thread. And your app really should return from `didFinishLaunching` before attempting any work, or you risk being terminated by the watchdog process. – Paulw11 Feb 18 '23 at 07:50
  • Thanks for answering ,I edited question to make it more spacific . I am trying to show user a error message in case user device has some configuration which need to be changed before starting app .This needs to be done for situation in which application is not able to reach to the part where it creates a rootviewcontroller and it is crashing before that only. – Abhishek Feb 18 '23 at 10:50
  • The first thing your app should do is create a root view controller. Even if it is just a placeholder or loading screen that will be replaced once set up is complete. And if your app crashes then you can't display anything. If you have a non-crash error then you can display an error, rebut there is no reason why there should not be a root view controller at that point. – Paulw11 Feb 18 '23 at 11:08
  • 1
    And your `AlertPopup` function is still reliant on there being a window and a root view controller. If there isn't then there is nothing to display the alert on. `window?!` is nonsense. You are either conditionally unwrapping or force unwrapping. You wouldn't do both. And the essential answer is still the same. You must show the left on the main thread. There will always be a main thread, so I don't understand your concern. – Paulw11 Feb 18 '23 at 11:13

1 Answers1

0

I tried a using a UIAlertController to present it from worker thread but xcode yelled at me to use it to call from main thread.Is there some other artefact which is thread safe

As a rule, anything that touches the UI has to run on the main thread. So the direct answer to your question is no, there's no other way to go about it.

That said, there shouldn't be any reason that you can't use the main thread. If you've got a worker thread that's deciding whether to show the alert, that's fine; when it has made it's decision, it just needs to signal the main thread to show the alert (or not). It sounds like you might be blocking the main thread waiting for the decision to be made? If so, don't do that. Just defer whatever you're doing on the main thread that should be deferred until after the decision comes down.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I am looking for a way to show user a error message in case ,application is not able to reach a point to set a rootviewcontroller ,crashes before that and need user to change configuration before they start application in his device again – Abhishek Feb 18 '23 at 10:57
  • You'll still need to do it from the main thread. – Caleb Feb 19 '23 at 03:11