OnApplicationPause can't run coroutine

Hey , I am building a game with firebase and I want to run a coroutine when the player is not in focus or pause
the OnApplicationPause does work but I can’t run the coroutine
is there a way for me to run the coroutine a bit before the app paused on mobile

thank you

Why can’t you run a coroutine? Are you getting an error, or is the coroutine just not working?

Show your code – and make sure to use the code tags: 8751237--1185708--upload_2023-1-23_12-55-41.png

the coroutine won’t even start , on the editor it does work only on mobile it won’t start the coroutine

    void OnApplicationPause(bool pauseStatus)
    {
        if (pauseStatus == true)
        {
            StartCoroutine(UpdateHost());
        }
    }
    private IEnumerator UpdateHost()
    {
        var task = db.Child("Users").Child(GameManager.instance.pinCode.ToString()).GetValueAsync();
        yield return new WaitUntil(predicate: () => task.IsCompleted);
        

        if (task.Exception != null)
        {
            Debug.Log("Something wrong");
            yield break;
        }
        else
        {
            DataSnapshot data = task.Result;

            foreach (DataSnapshot childSnap in data.Children)
            {
                if (childSnap.Key == currentPlayer.GetpublicID().ToString()) continue;
                if (childSnap.Child("isInFocus").Value is bool and true)
                {
                    currentPlayer.SetPlayerNetworkType(PlayerNetworkType.player);
                    db.Child("Users").Child(GameManager.instance.pinCode.ToString()).Child(childSnap.Key).Child("changeHost").SetValueAsync(true);
                    Debug.Log(childSnap.Child("username").Value + "  set as host");
                    host.text = " I am not host anymore " + childSnap.Child("username").Value.ToString() + " " + currentPlayer.GetPlayerNetworkType().ToString();
                    break;
                }
            }

        }
    }

Realistically you will likely only get one frame to do anything in OnApplicationPause.

You might get more depending on operating system and the reason for the application exit (eg, some mobile phones MIGHT let you run a few frames), but you really cannot even guarantee you’ll even get the actual OnApplicationPause, such as if the operating system panics or crashes or otherwise has to eject your process suddenly.

Structure your app to not rely on this callback.

Oh, right, this is on a mobile platform. I have limited experience here, but aren’t applications generally very restricted in what they can do when they put in the background?

I would check that OnApplicationPause is even getting called, though, by logging something after starting the coroutine.

1 Like

Thank you I have another option but its not the best one

do you have any idea how should or where I should run the coroutine before the player pause the system?

thank you
It does get called only the coroutine won’t

Do it when the player presses pause in your game.

Relying on external pause occurrence is not guaranteed to work. It might, but as you can already see it is fraught.

Thank you, the thing is sometimes players will exit the game via the mobile buttons and not from the menu
when they do that I want to run this coroutine to let the other players know that the user is not there anymore

my second option is to run the coroutine for everything someone minimizes the game and then save it to a list
and run the things that I need with this list

I’d suggest using “heartbeat”: every second, the game tells the server that it’s still running.

If you don’t see a heartbeat for a few seconds, you can assume the game is minimized.

I can’t do that I use firebase as my backend because it’s a 2d basic game
the only way is to an update when something change and then update the list