2

I want to save huge data in local Database on background but my unity IDE will be hang for 2-3 minutes when I try to save the data. After 2-3 minutes it's work perfectly .can anyone please suggest me how can i do that..Below is my sample code

private IEnumerator WaitAndPrint(TestController.TestReportModel report)
{
    dbHelper.deleteAllFromTable(dbHelper.TABLE_OFFLINE_MASTER_TEST_REPORT);
    dbHelper.deleteAllFromTable(dbHelper.TABLE_MASTER_OFFLINE_POINT_DATA);
    for (int i = 0; i < report.data.Count; i++)
    {
        TestController.TestData MasterData = report.data[i];
        dbHelper.AddOfflineMasterTestReport(MasterData, "");
    }                
    yield return new WaitForSeconds(1);   
}
Daxtron2
  • 1,239
  • 1
  • 11
  • 19

1 Answers1

1

Coroutine is not a Thread. If the saving operation is taking so much time to complete, create a new Thread and do it there. See the function below for an example. That code has been moved into a new Thread.

void WaitAndPrint(TestController.TestReportModel report)
{
    //Create Thread
    Thread thread = new Thread(delegate ()
    {
        dbHelper.deleteAllFromTable(dbHelper.TABLE_OFFLINE_MASTER_TEST_REPORT);
        dbHelper.deleteAllFromTable(dbHelper.TABLE_MASTER_OFFLINE_POINT_DATA);

        for (int i = 0; i < report.data.Count; i++)
        {
            TestController.TestData MasterData = report.data[i];
            dbHelper.AddOfflineMasterTestReport(MasterData, "");
        }
    });
    //Start the Thread and execute the code inside it
    thread.Start();
}

You can now call your function directly and it should not freeze Unity: WaitAndPrint(report);

If you want to use Unity's API from that new Threa, use UnityThread.executeInUpdate. See this for full example.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hello #Programmer. Your solution work perfectly as I want but in some cases like Database Operation it's throws Null Pointer Exception then please suggest how to handle this?? public void AddMasterTestReportPointData(string test, TestController model) { Thread thread = new Thread (delegate() { OpenDB (); string sqlQuery = "Insert Query" // Debug.Log("sqlQuery " + sqlQuery); dbcmd.CommandText = sqlQuery; reader = dbcmd.ExecuteReader (); CloseDB (); }); thread.Start (); } – shubham agarwal Apr 25 '18 at 06:13
  • The problem is from the database code. If it throws an error then it will also throw that error without the Thread code, You can find out which line is causing it by commenting out the code from bottom to up until you stop seeeing it but I wouldn't waste my time on this because that's not how to properly connect to a database in Unity. See [this](https://stackoverflow.com/questions/39140068/how-to-connect-to-database-from-unity/) for how to connect to database from Unity – Programmer Apr 25 '18 at 06:27
  • Without the thread code it's work perfectly but when i add the thread code it'll thrown an Null Pointer Exception. – shubham agarwal Apr 25 '18 at 10:18
  • If that's the case, please create a new question with your exact code that's causing that. I will take a look – Programmer Apr 25 '18 at 10:23