1

In a project that I will develop with Unity, I need to check the data coming from the web service instantly. If a new data entry has been made, I will display it as a notification.

The problem is: How can Unity check the data from the web service in the background? I have to do this without installing a huge load on the server.

Thank you.

kozyalcin
  • 83
  • 10
  • I assume you want to communicate to your web service via HTTP requests and want to do something right then when data in your database has changed. One approach to do this could be to send requests periodically. The Unity way to do that 'in the background' are coroutines (https://docs.unity3d.com/Manual/Coroutines.html), otherwise you might want to look into multithreading. – Benjamin Zach Dec 19 '19 at 11:14
  • Hi; thanks for your answer. But I have one more questions : When close the Application on phone screen; I want to run as the "android service" or check the web service and if new data in web service application must auto wake up for notification. – kozyalcin Dec 23 '19 at 14:11

1 Answers1

0

Unity has an InvokeRepeating method you could use, that will call a function every x seconds. Here's a sample code of what it would look like when using it with StartCoroutine:

    void Start()
    {
        //Call the CheckForUpdates function every 5 seconds
        InvokeRepeating("CheckForUpdates", 5.0f, 5.0f);
    }

    private void CheckForUpdates()
    {
        StartCoroutine(
            webservicesAPIcall()
        );
    }

    private IEnumerator webservicesAPIcall()
    {
        //make API calls to the web service in here
    }
will-yama
  • 680
  • 5
  • 10
  • Hi; thanks for your answer. But I have one more questions : When close the Application on phone screen; I want to run as the "android service" or check the web service and if new data in web service application must auto wake up for notification – kozyalcin Dec 23 '19 at 14:11
  • I must have misunderstood what you meant by "in the background" - I have no answer to give for when the background service is running in the Android. – will-yama Dec 25 '19 at 14:52