0

I am setting up a SQL server database and want to store continuous data from my Unity app while running through HTTP request.

I am using Unitywebrequest.post to send the data to the database through coroutine call. However, this only allows the data to be transferred one time.

Is there any other way Unitywebrequest.post can be used to send the data other than calling it through coroutine?

I tried to put Unitywebrequest.post in Update function to allow continuous increment data to be sent to the database while the app is running. But, the data that were logged in the database is not incremented properly as expected.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class StoreData : MonoBehaviour
{

int intDummyValue = 0;

    // Start is called before the first frame update
    void Start()
    {


    }

    // Update is called once per frame
    void Update()
    {

        updateDummyInt();
        Upload(intDummyValue.toString());


    }



    private void updateDummyInt()
    {
        intDummyValue += 1;
    }



    private void Upload(string dummy1)
    {

        WWWForm form = new WWWForm();



        form.AddField("intDummy", dummy1);

        UnityWebRequest www = UnityWebRequest.Post("http://localhost/logToDB.php", form);

        www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log("error is " + www.error);
        }
        else
        {
            Debug.Log("Data Logged");
        }





    }
}

I expected the column of intDummy to be 1,2,3,4,.... But the result I get is 1,2,3,14,15,4,16,5,....

deduu
  • 157
  • 5
  • 12
  • 1
    Sounds like a classical parallel programming issue. You have more than one co-routine running simultaneously. You need one thread that published data to the API in the order it is pushed. You can for example use the queue mentioned [here](https://stackoverflow.com/questions/12375339/threadsafe-fifo-queue-buffer) to build that. – fredrik Sep 23 '19 at 17:24
  • @fredrik what do you mean by "you have more than one co-routine running simultaneously"? as shown in the source code I don't use any co-routine at all. – deduu Sep 23 '19 at 18:34
  • Yes, but it could be running more than once at the same time. – fredrik Sep 23 '19 at 19:03

0 Answers0